| 1 | /* $Id: gen_object.cpp,v 1.13 2004-04-20 10:11:42 sandervl Exp $ */
|
|---|
| 2 | /*
|
|---|
| 3 | * Generic Object Class for OS/2
|
|---|
| 4 | *
|
|---|
| 5 | * Allocated in shared memory, so other processes can access the objects
|
|---|
| 6 | *
|
|---|
| 7 | * NOTE: Requires safety precautions to use objects in multiple threads or processes
|
|---|
| 8 | *
|
|---|
| 9 | * Copyright 1999 Sander van Leeuwen (sandervl@xs4all.nl)
|
|---|
| 10 | *
|
|---|
| 11 | */
|
|---|
| 12 | #define INCL_DOSSEMAPHORES
|
|---|
| 13 | #include <os2wrap.h>
|
|---|
| 14 | #include <stdlib.h>
|
|---|
| 15 | #include <misc.h>
|
|---|
| 16 | #include <win32type.h>
|
|---|
| 17 | #include <gen_object.h>
|
|---|
| 18 |
|
|---|
| 19 | #define DBG_LOCALLOG DBG_gen_object
|
|---|
| 20 | #include "dbglocal.h"
|
|---|
| 21 |
|
|---|
| 22 | //******************************************************************************
|
|---|
| 23 | //******************************************************************************
|
|---|
| 24 | GenericObject::GenericObject(GenericObject **head, VMutex *pLock)
|
|---|
| 25 | {
|
|---|
| 26 | this->pLock = pLock;
|
|---|
| 27 | this->head = head;
|
|---|
| 28 | this->next = NULL;
|
|---|
| 29 | refCount = 1;
|
|---|
| 30 |
|
|---|
| 31 | fLinked = FALSE;
|
|---|
| 32 | fDeletePending = FALSE;
|
|---|
| 33 |
|
|---|
| 34 | link();
|
|---|
| 35 | }
|
|---|
| 36 | //******************************************************************************
|
|---|
| 37 | //******************************************************************************
|
|---|
| 38 | GenericObject::~GenericObject()
|
|---|
| 39 | {
|
|---|
| 40 | unlink();
|
|---|
| 41 | }
|
|---|
| 42 | //******************************************************************************
|
|---|
| 43 | //******************************************************************************
|
|---|
| 44 | void GenericObject::link()
|
|---|
| 45 | {
|
|---|
| 46 | lock();
|
|---|
| 47 | if(*head == NULL) {
|
|---|
| 48 | *head = this;
|
|---|
| 49 | }
|
|---|
| 50 | else {
|
|---|
| 51 | GenericObject *cur = *head;
|
|---|
| 52 | while(cur->next)
|
|---|
| 53 | {
|
|---|
| 54 | cur = cur->next;
|
|---|
| 55 | }
|
|---|
| 56 | cur->next = this;
|
|---|
| 57 | }
|
|---|
| 58 | fLinked = TRUE;
|
|---|
| 59 | unlock();
|
|---|
| 60 | }
|
|---|
| 61 | //******************************************************************************
|
|---|
| 62 | //******************************************************************************
|
|---|
| 63 | void GenericObject::unlink()
|
|---|
| 64 | {
|
|---|
| 65 | if(!fLinked) return;
|
|---|
| 66 |
|
|---|
| 67 | //remove from linked list
|
|---|
| 68 | lock();
|
|---|
| 69 | if(*head == this) {
|
|---|
| 70 | *head = next;
|
|---|
| 71 | }
|
|---|
| 72 | else {
|
|---|
| 73 | GenericObject *cur = *head;
|
|---|
| 74 | while(cur->next != this)
|
|---|
| 75 | {
|
|---|
| 76 | cur = cur->next;
|
|---|
| 77 | if(cur == NULL) {
|
|---|
| 78 | dprintf(("GenericObject dtor: cur == NULL!!"));
|
|---|
| 79 | unlock();
|
|---|
| 80 | DebugInt3();
|
|---|
| 81 | return;
|
|---|
| 82 | }
|
|---|
| 83 | }
|
|---|
| 84 | cur->next = next;
|
|---|
| 85 | }
|
|---|
| 86 | unlock();
|
|---|
| 87 | }
|
|---|
| 88 | //******************************************************************************
|
|---|
| 89 | //******************************************************************************
|
|---|
| 90 | #ifdef DEBUG
|
|---|
| 91 | LONG GenericObject::addRef()
|
|---|
| 92 | {
|
|---|
| 93 | //// dprintf2(("addRef %x -> refcount %x", this, refCount+1));
|
|---|
| 94 | return InterlockedIncrement(&refCount);
|
|---|
| 95 | }
|
|---|
| 96 | #endif
|
|---|
| 97 | //******************************************************************************
|
|---|
| 98 | //******************************************************************************
|
|---|
| 99 | LONG GenericObject::release()
|
|---|
| 100 | {
|
|---|
| 101 | //// dprintf2(("release %x -> refcount %x", this, refCount-1));
|
|---|
| 102 | #ifdef DEBUG
|
|---|
| 103 | if(refCount-1 < 0) {
|
|---|
| 104 | DebugInt3();
|
|---|
| 105 | }
|
|---|
| 106 | #endif
|
|---|
| 107 | if(InterlockedDecrement(&refCount) == 0 && fDeletePending) {
|
|---|
| 108 | dprintf2(("marked for deletion -> delete now"));
|
|---|
| 109 | delete this;
|
|---|
| 110 | return 0;
|
|---|
| 111 | }
|
|---|
| 112 | return refCount;
|
|---|
| 113 | }
|
|---|
| 114 | //******************************************************************************
|
|---|
| 115 | //******************************************************************************
|
|---|
| 116 | void GenericObject::DestroyAll(GenericObject *head)
|
|---|
| 117 | {
|
|---|
| 118 | GenericObject *cur, *next;
|
|---|
| 119 |
|
|---|
| 120 | cur = head;
|
|---|
| 121 | while(cur) {
|
|---|
| 122 | next = cur->next;
|
|---|
| 123 | if(cur->getRefCount() != 0) {
|
|---|
| 124 | dprintf(("Refcount %d for object %x", cur->getRefCount(), cur));
|
|---|
| 125 | }
|
|---|
| 126 | delete cur;
|
|---|
| 127 | cur = next;
|
|---|
| 128 | }
|
|---|
| 129 | }
|
|---|
| 130 | //******************************************************************************
|
|---|
| 131 | //******************************************************************************
|
|---|