Overview
Comment: | runtime: Rewrite synchronized.m. |
---|---|
Downloads: | Tarball | ZIP archive | SQL archive |
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA3-256: |
f65ad672721eecd043851484347bc691 |
User & Date: | js on 2012-08-05 10:45:42 |
Other Links: | manifest | tags |
Context
2012-08-05
| ||
12:17 | Use -Wno-objc-root-class if available. check-in: 0c87d8f53b user: js tags: trunk | |
10:45 | runtime: Rewrite synchronized.m. check-in: f65ad67272 user: js tags: trunk | |
10:45 | Add of_rmutex_t, a reentrant mutex implementation. check-in: 1cb3d9fef9 user: js tags: trunk | |
Changes
Modified src/runtime/synchronized.m from [9fc3b19498] to [e89280c879].
︙ | ︙ | |||
14 15 16 17 18 19 20 | * file. */ #include "config.h" #include <stdio.h> #include <stdlib.h> | < < < | | < < | > | < < < < < < < < < < < | < < | > | | < < < < | < | < < | < < < | < < < | < < < | < < | | | | < | | < | < < | < < < < < | < | < | | | < < | < | < < | | | | < | < < < | < < | | | < < < | < < | < | | < < < | < | < < < < < | > | | < < | | < | < < < < < | | < | | | | | | < < | | 14 15 16 17 18 19 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 | * file. */ #include "config.h" #include <stdio.h> #include <stdlib.h> #import "runtime.h" #import "runtime-private.h" #import "threading.h" struct lock_s { id object; int count; of_rmutex_t rmutex; struct lock_s *next; } *locks = NULL; static of_mutex_t mutex; static void __attribute__((constructor)) init(void) { if (!of_mutex_new(&mutex)) OBJC_ERROR("Failed to create mutex!") } int objc_sync_enter(id object) { struct lock_s *lock; if (!of_mutex_lock(&mutex)) 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 (!of_mutex_unlock(&mutex)) OBJC_ERROR("Failed to unlock mutex!"); if (!of_rmutex_lock(&lock->rmutex)) 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 (!of_rmutex_new(&lock->rmutex)) OBJC_ERROR("Failed to create mutex!"); lock->object = object; lock->count = 1; lock->next = locks; locks = lock; if (!of_mutex_unlock(&mutex)) OBJC_ERROR("Failed to unlock mutex!"); if (!of_rmutex_lock(&lock->rmutex)) OBJC_ERROR("Failed to lock mutex!"); return 0; } int objc_sync_exit(id object) { struct lock_s *lock, *last = NULL; if (!of_mutex_lock(&mutex)) OBJC_ERROR("Failed to lock mutex!"); for (lock = locks; lock != NULL; lock = lock->next) { if (lock->object != object) { last = lock; continue; } if (!of_rmutex_unlock(&lock->rmutex)) OBJC_ERROR("Failed to unlock mutex!"); if (--lock->count == 0) { if (!of_rmutex_free(&lock->rmutex)) OBJC_ERROR("Failed to destroy mutex!"); if (last != NULL) last->next = lock->next; if (locks == lock) locks = lock->next; free(lock); } if (!of_mutex_unlock(&mutex)) OBJC_ERROR("Failed to unlock mutex!"); return 0; } OBJC_ERROR("objc_sync_exit() was called for an object not locked!"); } |