[10587] | 1 | /* $Id: gen_object.cpp,v 1.13 2004-04-20 10:11:42 sandervl Exp $ */
|
---|
[2469] | 2 | /*
|
---|
| 3 | * Generic Object Class for OS/2
|
---|
| 4 | *
|
---|
| 5 | * Allocated in shared memory, so other processes can access the objects
|
---|
[10587] | 6 | *
|
---|
[2469] | 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>
|
---|
[5083] | 15 | #include <misc.h>
|
---|
[2469] | 16 | #include <win32type.h>
|
---|
[21916] | 17 | #include "gen_object.h"
|
---|
[2469] | 18 |
|
---|
[2804] | 19 | #define DBG_LOCALLOG DBG_gen_object
|
---|
| 20 | #include "dbglocal.h"
|
---|
| 21 |
|
---|
[5935] | 22 | //******************************************************************************
|
---|
| 23 | //******************************************************************************
|
---|
[10587] | 24 | GenericObject::GenericObject(GenericObject **head, VMutex *pLock)
|
---|
[5935] | 25 | {
|
---|
| 26 | this->pLock = pLock;
|
---|
| 27 | this->head = head;
|
---|
| 28 | this->next = NULL;
|
---|
| 29 | refCount = 1;
|
---|
[10587] | 30 |
|
---|
[5935] | 31 | fLinked = FALSE;
|
---|
| 32 | fDeletePending = FALSE;
|
---|
[2469] | 33 |
|
---|
[5935] | 34 | link();
|
---|
| 35 | }
|
---|
[2469] | 36 | //******************************************************************************
|
---|
| 37 | //******************************************************************************
|
---|
[5935] | 38 | GenericObject::~GenericObject()
|
---|
[2469] | 39 | {
|
---|
[5935] | 40 | unlink();
|
---|
| 41 | }
|
---|
| 42 | //******************************************************************************
|
---|
| 43 | //******************************************************************************
|
---|
| 44 | void GenericObject::link()
|
---|
| 45 | {
|
---|
| 46 | lock();
|
---|
[2469] | 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 | }
|
---|
[5935] | 58 | fLinked = TRUE;
|
---|
| 59 | unlock();
|
---|
[2469] | 60 | }
|
---|
| 61 | //******************************************************************************
|
---|
| 62 | //******************************************************************************
|
---|
[5935] | 63 | void GenericObject::unlink()
|
---|
[2469] | 64 | {
|
---|
[5935] | 65 | if(!fLinked) return;
|
---|
| 66 |
|
---|
[2469] | 67 | //remove from linked list
|
---|
[5935] | 68 | lock();
|
---|
[2469] | 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!!"));
|
---|
[5935] | 79 | unlock();
|
---|
[2469] | 80 | DebugInt3();
|
---|
[5935] | 81 | return;
|
---|
[2469] | 82 | }
|
---|
| 83 | }
|
---|
| 84 | cur->next = next;
|
---|
| 85 | }
|
---|
[5935] | 86 | unlock();
|
---|
[2469] | 87 | }
|
---|
| 88 | //******************************************************************************
|
---|
| 89 | //******************************************************************************
|
---|
[5950] | 90 | #ifdef DEBUG
|
---|
| 91 | LONG GenericObject::addRef()
|
---|
| 92 | {
|
---|
[6375] | 93 | //// dprintf2(("addRef %x -> refcount %x", this, refCount+1));
|
---|
[5950] | 94 | return InterlockedIncrement(&refCount);
|
---|
| 95 | }
|
---|
| 96 | #endif
|
---|
| 97 | //******************************************************************************
|
---|
| 98 | //******************************************************************************
|
---|
[5935] | 99 | LONG GenericObject::release()
|
---|
| 100 | {
|
---|
[6375] | 101 | //// dprintf2(("release %x -> refcount %x", this, refCount-1));
|
---|
[5935] | 102 | #ifdef DEBUG
|
---|
| 103 | if(refCount-1 < 0) {
|
---|
| 104 | DebugInt3();
|
---|
| 105 | }
|
---|
| 106 | #endif
|
---|
| 107 | if(InterlockedDecrement(&refCount) == 0 && fDeletePending) {
|
---|
[5964] | 108 | dprintf2(("marked for deletion -> delete now"));
|
---|
[5935] | 109 | delete this;
|
---|
| 110 | return 0;
|
---|
| 111 | }
|
---|
| 112 | return refCount;
|
---|
| 113 | }
|
---|
| 114 | //******************************************************************************
|
---|
| 115 | //******************************************************************************
|
---|
[2469] | 116 | void GenericObject::DestroyAll(GenericObject *head)
|
---|
| 117 | {
|
---|
| 118 | GenericObject *cur, *next;
|
---|
| 119 |
|
---|
| 120 | cur = head;
|
---|
| 121 | while(cur) {
|
---|
| 122 | next = cur->next;
|
---|
[5935] | 123 | if(cur->getRefCount() != 0) {
|
---|
| 124 | dprintf(("Refcount %d for object %x", cur->getRefCount(), cur));
|
---|
| 125 | }
|
---|
[2469] | 126 | delete cur;
|
---|
| 127 | cur = next;
|
---|
| 128 | }
|
---|
| 129 | }
|
---|
| 130 | //******************************************************************************
|
---|
| 131 | //******************************************************************************
|
---|