 |
Changeset 1917
- Timestamp:
- 03/16/07 16:06:57
(2 years ago)
- Author:
- sean
- Message:
Fixes for issue #316.
-
Files:
-
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
| r454 |
r1917 |
|
| 1 | 1 | /* GDC -- D front-end for GCC |
|---|
| 2 | 2 | Copyright (C) 2004 David Friedman |
|---|
| 3 | | |
|---|
| | 3 | |
|---|
| 4 | 4 | This program is free software; you can redistribute it and/or modify |
|---|
| 5 | 5 | it under the terms of the GNU General Public License as published by |
|---|
| 6 | 6 | the Free Software Foundation; either version 2 of the License, or |
|---|
| 7 | 7 | (at your option) any later version. |
|---|
| 8 | | |
|---|
| | 8 | |
|---|
| 9 | 9 | This program is distributed in the hope that it will be useful, |
|---|
| 10 | 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 11 | 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 12 | 12 | GNU General Public License for more details. |
|---|
| 13 | | |
|---|
| | 13 | |
|---|
| 14 | 14 | You should have received a copy of the GNU General Public License |
|---|
| 15 | 15 | along with this program; if not, write to the Free Software |
|---|
| … | … | |
| 24 | 24 | private import tango.stdc.posix.semaphore; |
|---|
| 25 | 25 | struct Semaphore { |
|---|
| 26 | | sem_t sem; |
|---|
| 27 | | bool create() { return sem_init(& sem, 0, 0) == 0; } |
|---|
| 28 | | void wait() { sem_wait(& sem); } |
|---|
| 29 | | void signal() { sem_post(& sem); } |
|---|
| 30 | | |
|---|
| | 26 | sem_t sem; |
|---|
| | 27 | bool create() { return sem_init(& sem, 0, 0) == 0; } |
|---|
| | 28 | void wait() { sem_wait(& sem); } |
|---|
| | 29 | void signal() { sem_post(& sem); } |
|---|
| | 30 | |
|---|
| 31 | 31 | } |
|---|
| 32 | 32 | } |
|---|
| 33 | 33 | else version (GNU_Semaphore_Mach) |
|---|
| 34 | 34 | { |
|---|
| 35 | | // TODO: this needs a Tango replacement |
|---|
| 36 | | private import std.c.mach.mach; |
|---|
| | 35 | // from std.c.mach.mach |
|---|
| | 36 | |
|---|
| | 37 | extern (C) |
|---|
| | 38 | { |
|---|
| | 39 | enum |
|---|
| | 40 | { |
|---|
| | 41 | SYNC_POLICY_FIFO = 0x0, |
|---|
| | 42 | SYNC_POLICY_FIXED_PRIORITY = 0x1, |
|---|
| | 43 | SYNC_POLICY_REVERSED = 0x2, |
|---|
| | 44 | SYNC_POLICY_ORDER_MASK = 0x3, |
|---|
| | 45 | SYNC_POLICY_LIFO = (SYNC_POLICY_FIFO|SYNC_POLICY_REVERSED) |
|---|
| | 46 | } |
|---|
| | 47 | |
|---|
| | 48 | enum |
|---|
| | 49 | { |
|---|
| | 50 | KERN_SUCCESS = 0 |
|---|
| | 51 | } |
|---|
| | 52 | |
|---|
| | 53 | private alias uint natural_t; // uint on both 32- and 64-bit |
|---|
| | 54 | alias natural_t semaphore_t; // TODO: natural_t |
|---|
| | 55 | alias natural_t task_t; // TODO: natural_t |
|---|
| | 56 | alias natural_t mach_port_t; // TODO: natural_t |
|---|
| | 57 | alias int kern_return_t; |
|---|
| | 58 | |
|---|
| | 59 | // this probably isn't stable |
|---|
| | 60 | mach_port_t mach_task_self_; |
|---|
| | 61 | |
|---|
| | 62 | kern_return_t semaphore_create( task_t, semaphore_t*, int, int); |
|---|
| | 63 | kern_return_t semaphore_destroy( task_t, semaphore_t); |
|---|
| | 64 | kern_return_t semaphore_signal(semaphore_t); |
|---|
| | 65 | kern_return_t semaphore_wait(semaphore_t); |
|---|
| | 66 | } |
|---|
| | 67 | |
|---|
| | 68 | mach_port_t current_task() { return mach_task_self_; } |
|---|
| | 69 | |
|---|
| 37 | 70 | struct Semaphore { |
|---|
| 38 | | semaphore_t sem; |
|---|
| 39 | | bool create() { |
|---|
| 40 | | return semaphore_create(current_task(), & sem, |
|---|
| 41 | | SYNC_POLICY_FIFO, 0) == KERN_SUCCESS; } |
|---|
| 42 | | void wait() { semaphore_wait(sem); } |
|---|
| 43 | | void signal() { semaphore_signal(sem); } |
|---|
| | 71 | semaphore_t sem; |
|---|
| | 72 | bool create() { |
|---|
| | 73 | return semaphore_create(current_task(), & sem, |
|---|
| | 74 | SYNC_POLICY_FIFO, 0) == KERN_SUCCESS; } |
|---|
| | 75 | void wait() { semaphore_wait(sem); } |
|---|
| | 76 | void signal() { semaphore_signal(sem); } |
|---|
| 44 | 77 | } |
|---|
| 45 | 78 | } |
|---|
| … | … | |
| 47 | 80 | { |
|---|
| 48 | 81 | struct Semaphore { |
|---|
| 49 | | pthread_mutex_t lock; |
|---|
| 50 | | pthread_cond_t cond; |
|---|
| 51 | | int count; // boehm-gc only calls lock once -- outside the loop |
|---|
| 52 | | bool create() { |
|---|
| 53 | | count = 0; |
|---|
| 54 | | return pthread_mutex_init(& lock, null) == 0 && |
|---|
| 55 | | pthread_cond_init(& cond, null) == 0; |
|---|
| 56 | | } |
|---|
| 57 | | void wait() { |
|---|
| 58 | | // boehm-gc only calls lock once -- outside the loop |
|---|
| 59 | | pthread_mutex_lock(& lock); |
|---|
| 60 | | if (--count < 0) { |
|---|
| 61 | | while (count < 0) { // shouldn't be needed |
|---|
| 62 | | pthread_cond_wait(& cond, & lock); |
|---|
| 63 | | } |
|---|
| 64 | | } |
|---|
| 65 | | pthread_mutex_unlock(& lock); |
|---|
| 66 | | } |
|---|
| 67 | | void signal() { |
|---|
| 68 | | pthread_mutex_lock(& lock); |
|---|
| 69 | | if (++count >= 0) { |
|---|
| 70 | | pthread_cond_signal(& cond); |
|---|
| 71 | | } |
|---|
| 72 | | pthread_mutex_unlock(& lock); |
|---|
| 73 | | } |
|---|
| | 82 | pthread_mutex_t lock; |
|---|
| | 83 | pthread_cond_t cond; |
|---|
| | 84 | int count; // boehm-gc only calls lock once -- outside the loop |
|---|
| | 85 | bool create() { |
|---|
| | 86 | count = 0; |
|---|
| | 87 | return pthread_mutex_init(& lock, null) == 0 && |
|---|
| | 88 | pthread_cond_init(& cond, null) == 0; |
|---|
| | 89 | } |
|---|
| | 90 | void wait() { |
|---|
| | 91 | // boehm-gc only calls lock once -- outside the loop |
|---|
| | 92 | pthread_mutex_lock(& lock); |
|---|
| | 93 | if (--count < 0) { |
|---|
| | 94 | while (count < 0) { // shouldn't be needed |
|---|
| | 95 | pthread_cond_wait(& cond, & lock); |
|---|
| | 96 | } |
|---|
| | 97 | } |
|---|
| | 98 | pthread_mutex_unlock(& lock); |
|---|
| | 99 | } |
|---|
| | 100 | void signal() { |
|---|
| | 101 | pthread_mutex_lock(& lock); |
|---|
| | 102 | if (++count >= 0) { |
|---|
| | 103 | pthread_cond_signal(& cond); |
|---|
| | 104 | } |
|---|
| | 105 | pthread_mutex_unlock(& lock); |
|---|
| | 106 | } |
|---|
| 74 | 107 | } |
|---|
| 75 | 108 | } |
|---|
Download in other formats:
|
 |
 |
|
 |
Copyright © 2006-2008 Tango. All Rights Reserved. | Page Width:
Static or
Dynamic