git-svn-id: svn://qnap.local/TwoTerm/trunk@1650 5590a31f-7b70-45f8-8c82-aa3a8e5f4507

This commit is contained in:
Kelvin Sherlock
2010-07-09 01:18:30 +00:00
parent 0ea3270420
commit 829183a0f3
17 changed files with 1222 additions and 243 deletions
+48
View File
@@ -0,0 +1,48 @@
/*
* Benaphore.cpp
* Blue Box
*
* Created by Kelvin Sherlock on 11/29/2009.
* Copyright 2009 Kelvin W Sherlock LLC. All rights reserved.
*
*/
#include "Benaphore.h"
#include <libkern/OSAtomic.h>
#include <mach/semaphore.h>
#include <mach/task.h>
#include <mach/mach_init.h>
#include <cstdio>
Benaphore::Benaphore()
{
_atom = 0;
semaphore_create(mach_task_self(), (semaphore_t *)&_sem, SYNC_POLICY_FIFO, 0);
}
Benaphore::~Benaphore()
{
semaphore_destroy(mach_task_self(), (semaphore_t)_sem);
}
void Benaphore::lock()
{
// returns new value
if (OSAtomicIncrement32Barrier(&_atom) > 1)
{
fprintf(stderr, "waiting %u\n", _atom);
semaphore_wait((semaphore_t)_sem);
}
}
void Benaphore::unlock()
{
if (OSAtomicDecrement32Barrier(&_atom) > 0)
{
fprintf(stderr, "releasing %u\n", _atom);
semaphore_signal((semaphore_t)_sem);
}
}