Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact

Changeset 1917

Show
Ignore:
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
  • trunk/lib/compiler/gdc/gcc/threadsem.d

    r454 r1917  
    11/* GDC -- D front-end for GCC 
    22   Copyright (C) 2004 David Friedman 
    3     
     3 
    44   This program is free software; you can redistribute it and/or modify 
    55   it under the terms of the GNU General Public License as published by 
    66   the Free Software Foundation; either version 2 of the License, or 
    77   (at your option) any later version. 
    8   
     8 
    99   This program is distributed in the hope that it will be useful, 
    1010   but WITHOUT ANY WARRANTY; without even the implied warranty of 
    1111   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
    1212   GNU General Public License for more details. 
    13   
     13 
    1414   You should have received a copy of the GNU General Public License 
    1515   along with this program; if not, write to the Free Software 
     
    2424    private import tango.stdc.posix.semaphore; 
    2525    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 
    3131    } 
    3232} 
    3333else version (GNU_Semaphore_Mach) 
    3434{ 
    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 
    3770    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); } 
    4477    } 
    4578} 
     
    4780{ 
    4881    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       
    74107    } 
    75108}