Blame view

trunk/research/st/sync.c 7.7 KB
winlin authored
1 2 3 4 5 6 7 8 9 10 11 12 13 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
/* 
 * The contents of this file are subject to the Mozilla Public
 * License Version 1.1 (the "License"); you may not use this file
 * except in compliance with the License. You may obtain a copy of
 * the License at http://www.mozilla.org/MPL/
 * 
 * Software distributed under the License is distributed on an "AS
 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * rights and limitations under the License.
 * 
 * The Original Code is the Netscape Portable Runtime library.
 * 
 * The Initial Developer of the Original Code is Netscape
 * Communications Corporation.  Portions created by Netscape are 
 * Copyright (C) 1994-2000 Netscape Communications Corporation.  All
 * Rights Reserved.
 * 
 * Contributor(s):  Silicon Graphics, Inc.
 * 
 * Portions created by SGI are Copyright (C) 2000-2001 Silicon
 * Graphics, Inc.  All Rights Reserved.
 * 
 * Alternatively, the contents of this file may be used under the
 * terms of the GNU General Public License Version 2 or later (the
 * "GPL"), in which case the provisions of the GPL are applicable 
 * instead of those above.  If you wish to allow use of your 
 * version of this file only under the terms of the GPL and not to
 * allow others to use your version of this file under the MPL,
 * indicate your decision by deleting the provisions above and
 * replace them with the notice and other provisions required by
 * the GPL.  If you do not delete the provisions above, a recipient
 * may use your version of this file under either the MPL or the
 * GPL.
 */

/*
 * This file is derived directly from Netscape Communications Corporation,
 * and consists of extensive modifications made during the year(s) 1999-2000.
 */

#include <stdlib.h>
#include <time.h>
#include <errno.h>
#include "common.h"

extern time_t _st_curr_time;
extern st_utime_t _st_last_tset;
extern int _st_active_count;

static st_utime_t (*_st_utime)(void) = NULL;

/*****************************************
 * Time functions
 */

st_utime_t st_utime(void)
{
winlin authored
59
    if (_st_utime == NULL) {
winlin authored
60
#ifdef MD_GET_UTIME
winlin authored
61
        MD_GET_UTIME();
winlin authored
62
#else
winlin authored
63
        #error Unknown OS
winlin authored
64
#endif
winlin authored
65 66 67
    }
    
    return (*_st_utime)();
winlin authored
68 69 70 71
}

int st_set_utime_function(st_utime_t (*func)(void))
{
winlin authored
72 73 74 75 76 77 78 79
    if (_st_active_count) {
        errno = EINVAL;
        return -1;
    }
    
    _st_utime = func;
    
    return 0;
winlin authored
80 81 82 83
}

st_utime_t st_utime_last_clock(void)
{
winlin authored
84
    return _ST_LAST_CLOCK;
winlin authored
85 86 87 88
}

int st_timecache_set(int on)
{
winlin authored
89 90 91 92 93 94 95 96 97 98
    int wason = (_st_curr_time) ? 1 : 0;
    
    if (on) {
        _st_curr_time = time(NULL);
        _st_last_tset = st_utime();
    } else {
        _st_curr_time = 0;
    }
    
    return wason;
winlin authored
99 100 101 102
}

time_t st_time(void)
{
winlin authored
103 104 105 106 107
    if (_st_curr_time) {
        return _st_curr_time;
    }
    
    return time(NULL);
winlin authored
108 109 110 111
}

int st_usleep(st_utime_t usecs)
{
winlin authored
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
    _st_thread_t *me = _ST_CURRENT_THREAD();
    
    if (me->flags & _ST_FL_INTERRUPT) {
        me->flags &= ~_ST_FL_INTERRUPT;
        errno = EINTR;
        return -1;
    }
    
    if (usecs != ST_UTIME_NO_TIMEOUT) {
        me->state = _ST_ST_SLEEPING;
        _ST_ADD_SLEEPQ(me, usecs);
    } else {
        me->state = _ST_ST_SUSPENDED;
    }
    
    _ST_SWITCH_CONTEXT(me);
    
    if (me->flags & _ST_FL_INTERRUPT) {
        me->flags &= ~_ST_FL_INTERRUPT;
        errno = EINTR;
        return -1;
    }
    
    return 0;
winlin authored
136 137 138 139
}

int st_sleep(int secs)
{
winlin authored
140
    return st_usleep((secs >= 0) ? secs * (st_utime_t) 1000000LL : ST_UTIME_NO_TIMEOUT);
winlin authored
141 142 143 144 145 146 147
}

/*****************************************
 * Condition variable functions
 */
_st_cond_t *st_cond_new(void)
{
winlin authored
148 149 150 151 152 153 154 155
    _st_cond_t *cvar;
    
    cvar = (_st_cond_t *) calloc(1, sizeof(_st_cond_t));
    if (cvar) {
        ST_INIT_CLIST(&cvar->wait_q);
    }
    
    return cvar;
winlin authored
156 157 158 159
}

int st_cond_destroy(_st_cond_t *cvar)
{
winlin authored
160 161 162 163 164 165 166 167
    if (cvar->wait_q.next != &cvar->wait_q) {
        errno = EBUSY;
        return -1;
    }
    
    free(cvar);
    
    return 0;
winlin authored
168 169 170 171
}

int st_cond_timedwait(_st_cond_t *cvar, st_utime_t timeout)
{
winlin authored
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
    _st_thread_t *me = _ST_CURRENT_THREAD();
    int rv;
    
    if (me->flags & _ST_FL_INTERRUPT) {
        me->flags &= ~_ST_FL_INTERRUPT;
        errno = EINTR;
        return -1;
    }
    
    /* Put caller thread on the condition variable's wait queue */
    me->state = _ST_ST_COND_WAIT;
    ST_APPEND_LINK(&me->wait_links, &cvar->wait_q);
    
    if (timeout != ST_UTIME_NO_TIMEOUT) {
        _ST_ADD_SLEEPQ(me, timeout);
    }
    
    _ST_SWITCH_CONTEXT(me);
    
    ST_REMOVE_LINK(&me->wait_links);
    rv = 0;
    
    if (me->flags & _ST_FL_TIMEDOUT) {
        me->flags &= ~_ST_FL_TIMEDOUT;
        errno = ETIME;
        rv = -1;
    }
    if (me->flags & _ST_FL_INTERRUPT) {
        me->flags &= ~_ST_FL_INTERRUPT;
        errno = EINTR;
        rv = -1;
    }
    
    return rv;
winlin authored
206 207 208 209
}

int st_cond_wait(_st_cond_t *cvar)
{
winlin authored
210
    return st_cond_timedwait(cvar, ST_UTIME_NO_TIMEOUT);
winlin authored
211 212 213 214
}

static int _st_cond_signal(_st_cond_t *cvar, int broadcast)
{
winlin authored
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
    _st_thread_t *thread;
    _st_clist_t *q;
    
    for (q = cvar->wait_q.next; q != &cvar->wait_q; q = q->next) {
        thread = _ST_THREAD_WAITQ_PTR(q);
        if (thread->state == _ST_ST_COND_WAIT) {
            if (thread->flags & _ST_FL_ON_SLEEPQ) {
                _ST_DEL_SLEEPQ(thread);
            }
            
            /* Make thread runnable */
            thread->state = _ST_ST_RUNNABLE;
            _ST_ADD_RUNQ(thread);
            if (!broadcast) {
                break;
            }
        }
winlin authored
232
    }
winlin authored
233 234
    
    return 0;
winlin authored
235 236 237 238
}

int st_cond_signal(_st_cond_t *cvar)
{
winlin authored
239
    return _st_cond_signal(cvar, 0);
winlin authored
240 241 242 243
}

int st_cond_broadcast(_st_cond_t *cvar)
{
winlin authored
244
    return _st_cond_signal(cvar, 1);
winlin authored
245 246 247 248 249 250 251
}

/*****************************************
 * Mutex functions
 */
_st_mutex_t *st_mutex_new(void)
{
winlin authored
252 253 254 255 256 257 258 259 260
    _st_mutex_t *lock;
    
    lock = (_st_mutex_t *) calloc(1, sizeof(_st_mutex_t));
    if (lock) {
        ST_INIT_CLIST(&lock->wait_q);
        lock->owner = NULL;
    }
    
    return lock;
winlin authored
261 262 263 264
}

int st_mutex_destroy(_st_mutex_t *lock)
{
winlin authored
265 266 267 268 269 270 271 272
    if (lock->owner != NULL || lock->wait_q.next != &lock->wait_q) {
        errno = EBUSY;
        return -1;
    }
    
    free(lock);
    
    return 0;
winlin authored
273 274 275 276
}

int st_mutex_lock(_st_mutex_t *lock)
{
winlin authored
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
    _st_thread_t *me = _ST_CURRENT_THREAD();
    
    if (me->flags & _ST_FL_INTERRUPT) {
        me->flags &= ~_ST_FL_INTERRUPT;
        errno = EINTR;
        return -1;
    }
    
    if (lock->owner == NULL) {
        /* Got the mutex */
        lock->owner = me;
        return 0;
    }
    
    if (lock->owner == me) {
        errno = EDEADLK;
        return -1;
    }
    
    /* Put caller thread on the mutex's wait queue */
    me->state = _ST_ST_LOCK_WAIT;
    ST_APPEND_LINK(&me->wait_links, &lock->wait_q);
    
    _ST_SWITCH_CONTEXT(me);
    
    ST_REMOVE_LINK(&me->wait_links);
    
    if ((me->flags & _ST_FL_INTERRUPT) && lock->owner != me) {
        me->flags &= ~_ST_FL_INTERRUPT;
        errno = EINTR;
        return -1;
    }
    
winlin authored
310 311 312 313 314
    return 0;
}

int st_mutex_unlock(_st_mutex_t *lock)
{
winlin authored
315 316 317 318 319 320
    _st_thread_t *thread;
    _st_clist_t *q;
    
    if (lock->owner != _ST_CURRENT_THREAD()) {
        errno = EPERM;
        return -1;
winlin authored
321
    }
winlin authored
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
    
    for (q = lock->wait_q.next; q != &lock->wait_q; q = q->next) {
        thread = _ST_THREAD_WAITQ_PTR(q);
        if (thread->state == _ST_ST_LOCK_WAIT) {
            lock->owner = thread;
            /* Make thread runnable */
            thread->state = _ST_ST_RUNNABLE;
            _ST_ADD_RUNQ(thread);
            return 0;
        }
    }
    
    /* No threads waiting on this mutex */
    lock->owner = NULL;
    
    return 0;
winlin authored
338 339 340 341
}

int st_mutex_trylock(_st_mutex_t *lock)
{
winlin authored
342 343 344 345 346 347 348 349 350
    if (lock->owner != NULL) {
        errno = EBUSY;
        return -1;
    }
    
    /* Got the mutex */
    lock->owner = _ST_CURRENT_THREAD();
    
    return 0;
winlin authored
351 352
}