|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2024 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +#include "PlatformMutex.h" |
| 14 | +#include <stdlib.h> |
| 15 | + |
| 16 | +#if defined(__APPLE__) |
| 17 | +#include <os/lock.h> |
| 18 | + |
| 19 | +PlatformMutex swiftsyntax_platform_mutex_create() { |
| 20 | + PlatformMutex m; |
| 21 | + m.opaque = malloc(sizeof(os_unfair_lock)); |
| 22 | + *(os_unfair_lock_t)m.opaque = OS_UNFAIR_LOCK_INIT; |
| 23 | + return m; |
| 24 | +} |
| 25 | + |
| 26 | +void swiftsyntax_platform_mutex_lock(PlatformMutex m) { |
| 27 | + os_unfair_lock_lock(m.opaque); |
| 28 | +} |
| 29 | + |
| 30 | +void swiftsyntax_platform_mutex_unlock(PlatformMutex m) { |
| 31 | + os_unfair_lock_unlock(m.opaque); |
| 32 | +} |
| 33 | + |
| 34 | +void swiftsyntax_platform_mutex_destroy(PlatformMutex m) { |
| 35 | + free(m.opaque); |
| 36 | +} |
| 37 | + |
| 38 | +#elif defined(_WIN32) |
| 39 | +#include <synchapi.h> |
| 40 | + |
| 41 | +PlatformMutex swiftsyntax_platform_mutex_create() { |
| 42 | + PlatformMutex m; |
| 43 | + m.opaque = malloc(sizeof(SRWLOCK)); |
| 44 | + InitializeSRWLock(m.opaque); |
| 45 | + return m; |
| 46 | +} |
| 47 | + |
| 48 | +void swiftsyntax_platform_mutex_lock(PlatformMutex m) { |
| 49 | + AcquireSRWLockExclusive(m.opaque); |
| 50 | +} |
| 51 | + |
| 52 | +void swiftsyntax_platform_mutex_unlock(PlatformMutex m) { |
| 53 | + ReleaseSRWLockExclusive(m.opaque); |
| 54 | +} |
| 55 | + |
| 56 | +void swiftsyntax_platform_mutex_destroy(PlatformMutex m) { |
| 57 | + free(m.opaque); |
| 58 | +} |
| 59 | + |
| 60 | +#elif __has_include(<pthread.h>) |
| 61 | +#include <pthread.h> |
| 62 | + |
| 63 | +PlatformMutex swiftsyntax_platform_mutex_create() { |
| 64 | + PlatformMutex m; |
| 65 | + m.opaque = malloc(sizeof(pthread_mutex_t)); |
| 66 | + pthread_mutex_init(m.opaque, 0); |
| 67 | + return m; |
| 68 | +} |
| 69 | + |
| 70 | +void swiftsyntax_platform_mutex_lock(PlatformMutex m) { |
| 71 | + pthread_mutex_lock(m.opaque); |
| 72 | +} |
| 73 | + |
| 74 | +void swiftsyntax_platform_mutex_unlock(PlatformMutex m) { |
| 75 | + pthread_mutex_unlock(m.opaque); |
| 76 | +} |
| 77 | + |
| 78 | +void swiftsyntax_platform_mutex_destroy(PlatformMutex m) { |
| 79 | + pthread_mutex_destroy(m.opaque); |
| 80 | + free(m.opaque); |
| 81 | +} |
| 82 | + |
| 83 | +#else |
| 84 | +#warning "platfrom mutex implementation is not available. Assuming single thread" |
| 85 | + |
| 86 | +PlatformMutex swiftsyntax_platform_mutex_create() { |
| 87 | + PlatformMutex m; |
| 88 | + m.opaque = 0; |
| 89 | + return m; |
| 90 | +} |
| 91 | + |
| 92 | +void swiftsyntax_platform_mutex_lock(PlatformMutex m) {} |
| 93 | +void swiftsyntax_platform_mutex_unlock(PlatformMutex m) {} |
| 94 | +void swiftsyntax_platform_mutex_destroy(PlatformMutex m) {} |
| 95 | + |
| 96 | +#endif |
0 commit comments