Differences From Artifact [fe8bbba7b5]:
- File src/runtime/synchronized.m — part of check-in [374e1a1bfa] at 2021-01-02 22:04:26 on branch trunk — Update copyright (user: js, size: 2733) [annotate] [blame] [check-ins using] [more...]
To Artifact [0034717236]:
- File
src/runtime/synchronized.m
— part of check-in
[dfd52d5220]
at
2021-04-17 16:24:13
on branch new-naming-convention
— of_mutex_t -> OFPlainMutex
Also rename of_rmutex_t -> OFPlainRecursiveMutex. (user: js, size: 2796) [annotate] [blame] [check-ins using]
︙ | ︙ | |||
20 21 22 23 24 25 26 | #import "ObjFWRT.h" #import "private.h" #ifdef OF_HAVE_THREADS # import "mutex.h" | | | | | | | | | | | | | | | | | | | | | 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 | #import "ObjFWRT.h" #import "private.h" #ifdef OF_HAVE_THREADS # import "mutex.h" static struct lock { id object; int count; OFPlainRecursiveMutex rmutex; struct lock *next; } *locks = NULL; static OFPlainMutex mutex; OF_CONSTRUCTOR() { if (OFPlainMutexNew(&mutex) != 0) OBJC_ERROR("Failed to create mutex!"); } #endif int objc_sync_enter(id object) { if (object == nil) return 0; #ifdef OF_HAVE_THREADS struct lock *lock; if (OFPlainMutexLock(&mutex) != 0) OBJC_ERROR("Failed to lock mutex!"); /* Look if we already have a lock */ for (lock = locks; lock != NULL; lock = lock->next) { if (lock->object != object) continue; lock->count++; if (OFPlainMutexUnlock(&mutex) != 0) OBJC_ERROR("Failed to unlock mutex!"); if (OFPlainRecursiveMutexLock(&lock->rmutex) != 0) OBJC_ERROR("Failed to lock mutex!"); return 0; } /* Create a new lock */ if ((lock = malloc(sizeof(*lock))) == NULL) OBJC_ERROR("Failed to allocate memory for mutex!"); if (OFPlainRecursiveMutexNew(&lock->rmutex) != 0) OBJC_ERROR("Failed to create mutex!"); lock->object = object; lock->count = 1; lock->next = locks; locks = lock; if (OFPlainMutexUnlock(&mutex) != 0) OBJC_ERROR("Failed to unlock mutex!"); if (OFPlainRecursiveMutexLock(&lock->rmutex) != 0) OBJC_ERROR("Failed to lock mutex!"); #endif return 0; } int objc_sync_exit(id object) { if (object == nil) return 0; #ifdef OF_HAVE_THREADS struct lock *lock, *last = NULL; if (OFPlainMutexLock(&mutex) != 0) OBJC_ERROR("Failed to lock mutex!"); for (lock = locks; lock != NULL; lock = lock->next) { if (lock->object != object) { last = lock; continue; } if (OFPlainRecursiveMutexUnlock(&lock->rmutex) != 0) OBJC_ERROR("Failed to unlock mutex!"); if (--lock->count == 0) { if (OFPlainRecursiveMutexFree(&lock->rmutex) != 0) OBJC_ERROR("Failed to destroy mutex!"); if (last != NULL) last->next = lock->next; if (locks == lock) locks = lock->next; free(lock); } if (OFPlainMutexUnlock(&mutex) != 0) OBJC_ERROR("Failed to unlock mutex!"); return 0; } OBJC_ERROR("objc_sync_exit() was called for an object not locked!"); #else return 0; #endif } |