<commit_before>/*
 * OpenMAMA: The open middleware agnostic messaging API
 * Copyright (C) 2011 NYSE Technologies, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301 USA
 */

#include "wombat/wSemaphore.h"
#include <sys/errno.h>
#include <libkern/OSAtomic.h>


#define WSEM_CHECK_NULL(wsem_t)     \
    if(!wsem_t) {                   \
        errno = EFAULT;             \
        return WSEM_FAILED;         \
    }                               \


int
wsem_init(wsem_t * sem, int shared, unsigned int value) {
    WSEM_CHECK_NULL(sem);

    sem->dsema = dispatch_semaphore_create(value);
    if(!sem->dsema)
        return WSEM_FAILED;

    sem->count = value;
    return WSEM_SUCCEED;

}

int
wsem_destroy(wsem_t * sem)  {
    WSEM_CHECK_NULL(sem);
    dispatch_release(sem->dsema);
    return WSEM_SUCCEED;
}

int
wsem_getvalue(wsem_t * sem, int * i) {
    WSEM_CHECK_NULL(sem);
    (*i) = sem->count;
    return WSEM_SUCCEED;
}

int
wsem_post(wsem_t * sem) {
    WSEM_CHECK_NULL(sem);
    int result;

    if(dispatch_semaphore_signal(sem->dsema) == 0) {
        OSAtomicIncrement32(&sem->count);
        result =  WSEM_SUCCEED;
    }
    else
        result = WSEM_FAILED;

    return result;
}

int wsem_wait(wsem_t * sem) {
    WSEM_CHECK_NULL(sem);
    int result;

    if (dispatch_semaphore_wait(sem->dsema, DISPATCH_TIME_FOREVER) == 0) {
        OSAtomicDecrement32(&sem->count);
        result = WSEM_SUCCEED;
    }
    else
        result = WSEM_FAILED;

    return result;
}

int wsem_trywait(wsem_t * sem) {
    WSEM_CHECK_NULL(sem);
    int result;

    if (dispatch_semaphore_wait(sem->dsema, DISPATCH_TIME_NOW) == 0) {
        OSAtomicDecrement32(&sem->count);
        result = WSEM_SUCCEED;
    }
    else
        result = WSEM_FAILED;

    return result;
}

int wsem_timedwait (wsem_t* sem, unsigned int ts) {
    WSEM_CHECK_NULL(sem);
    int result;

    if (dispatch_semaphore_wait(sem->dsema,
                                dispatch_time(DISPATCH_TIME_NOW, ts * 1000)) == 0) {
        OSAtomicDecrement32(&sem->count);
        result = WSEM_SUCCEED;
    }
    else
       result = WSEM_FAILED;

    return result;
}<commit_msg>COMMON Darwin Semaphore Bug<commit_after>/*
 * OpenMAMA: The open middleware agnostic messaging API
 * Copyright (C) 2011 NYSE Technologies, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301 USA
 */

#include "wombat/wSemaphore.h"
#include <sys/errno.h>
#include <libkern/OSAtomic.h>


#define WSEM_CHECK_NULL(wsem_t)     \
    if(!wsem_t) {                   \
        errno = EFAULT;             \
        return WSEM_FAILED;         \
    }                               \


int
wsem_init(wsem_t * sem, int shared, unsigned int value) {
    WSEM_CHECK_NULL(sem);

    sem->dsema = dispatch_semaphore_create(value);
    if(!sem->dsema)
        return WSEM_FAILED;

    sem->count = value;
    return WSEM_SUCCEED;

}

int
wsem_destroy(wsem_t * sem)  {
    WSEM_CHECK_NULL(sem);
    dispatch_release(sem->dsema);
    return WSEM_SUCCEED;
}

int
wsem_getvalue(wsem_t * sem, int * i) {
    WSEM_CHECK_NULL(sem);
    (*i) = sem->count;
    return WSEM_SUCCEED;
}

int
wsem_post(wsem_t * sem) {
    WSEM_CHECK_NULL(sem);
    int result;

    if(dispatch_semaphore_signal(sem->dsema) == 0) {
        OSAtomicIncrement32Barrier(&sem->count);
        result =  WSEM_SUCCEED;
    }
    else
        result = WSEM_FAILED;

    return result;
}

int wsem_wait(wsem_t * sem) {
    WSEM_CHECK_NULL(sem);
    int result;

    if (dispatch_semaphore_wait(sem->dsema, DISPATCH_TIME_FOREVER) == 0) {
        if(sem->count)
            OSAtomicDecrement32Barrier(&sem->count);
        result = WSEM_SUCCEED;
    }
    else
        result = WSEM_FAILED;

    return result;
}

int wsem_trywait(wsem_t * sem) {
    WSEM_CHECK_NULL(sem);
    int result;

    if (dispatch_semaphore_wait(sem->dsema, DISPATCH_TIME_NOW) == 0) {
        OSAtomicDecrement32Barrier(&sem->count);
        result = WSEM_SUCCEED;
    }
    else
        result = WSEM_FAILED;

    return result;
}

int wsem_timedwait (wsem_t* sem, unsigned int ts) {
    WSEM_CHECK_NULL(sem);
    int result;

    if (dispatch_semaphore_wait(sem->dsema,
                                dispatch_time(DISPATCH_TIME_NOW, ts * 1000)) == 0) {
        if(sem->count)
            OSAtomicDecrement32Barrier(&sem->count);
        result = WSEM_SUCCEED;
    }
    else
        result = WSEM_FAILED;

    return result;
}
{'fraction_non_alphanumeric': 0.09380927746163131, 'fraction_numerical': 0.011381272633212623, 'mean_word_length': 2.677869372225745, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4769171', 'n_tokens_mistral': 2091, 'n_tokens_neox': 1969, 'n_words': 513}