示例#1
0
/*
    Lock a mutex
 */
PUBLIC void mprSpinLock(MprSpin *lock)
{
    if (lock == 0) return;

#if ME_DEBUG
    /*
        Spin locks don't support recursive locking on all operating systems.
     */
    assert(lock->owner != mprGetCurrentOsThread());
#endif

#if USE_MPR_LOCK
    mprTryLock(&lock->cs);
#elif MACOSX
    OSSpinLockLock(&lock->cs);
#elif ME_UNIX_LIKE && ME_COMPILER_HAS_SPINLOCK
    pthread_spin_lock(&lock->cs);
#elif ME_UNIX_LIKE
    pthread_mutex_lock(&lock->cs);
#elif ME_WIN_LIKE
    if (!lock->freed) {
        EnterCriticalSection(&lock->cs);
    }
#elif VXWORKS
    semTake(lock->cs, WAIT_FOREVER);
#endif
#if ME_DEBUG
    assert(lock->owner != mprGetCurrentOsThread());
    lock->owner = mprGetCurrentOsThread();
#endif
}
CoreAudioOutput::~CoreAudioOutput()
{
	OSSpinLockLock(_spinlockAU);
	
	if(_au != NULL)
	{
		AudioOutputUnitStop(_au);
		AudioUnitUninitialize(_au);
#if defined(MAC_OS_X_VERSION_10_6) && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
		if (IsOSXVersionSupported(10, 6, 0))
		{
			AudioComponentInstanceDispose(_au);
		}
		else
		{
			CloseComponent(_au);
		}
#else
		CloseComponent(_au);
#endif
		_au = NULL;
	}
	
	OSSpinLockUnlock(_spinlockAU);
	
	delete _buffer;
	_buffer = NULL;
	
	free(_spinlockAU);
	_spinlockAU = NULL;
}
示例#3
0
int main(int argc, char** argv) {
    char* service_name = "AppleOscarGyro";
    int client_type = 0;

    io_service_t service = IOServiceGetMatchingService(kIOMasterPortDefault, IOServiceMatching(service_name));
    if (service == MACH_PORT_NULL) {
        printf("can't find service\n");
        return 0;
    }

    IOServiceOpen(service, mach_task_self(), client_type, &conn);
    if (conn == MACH_PORT_NULL) {
        printf("can't connect to service\n");
        return 0;
    }

    OSSpinLockLock(&lock);

    pthread_t t;
    io_connect_t arg = conn;
    pthread_create(&t, NULL, (void*) go, (void*) &arg);

    usleep(100000);

    OSSpinLockUnlock(&lock);

    close_it(conn);

    pthread_join(t, NULL);

    return 0;
}
/**
 * Remove a specific entry node from the list.
 *
 * @param deleted_node The node to be removed.
 *
 * @warning This method is not async safe.
 */
template <typename V> void async_list<V>::nasync_remove_node (node *deleted_node) {
    /* Lock the list from other writers. */
    OSSpinLockLock(&_write_lock); {
        /* Find the record. */
        node *item = _head;
        while (item != NULL) {
            if (item == deleted_node)
                break;
            
            item = item->_next;
        }
        
        /* If not found, nothing to do */
        if (item == NULL) {
            OSSpinLockUnlock(&_write_lock);
            return;
        }
        
        /*
         * Atomically make the item unreachable by readers.
         *
         * This serves as a synchronization point -- after the CAS, the item is no longer reachable via the list.
         */
        if (item == _head) {
            if (!OSAtomicCompareAndSwapPtrBarrier(item, item->_next, (void **) &_head)) {
                PLCF_DEBUG("Failed to remove image list head despite holding lock");
            }
        } else {
            /* There MUST be a non-NULL prev pointer, as this is not HEAD. */
            if (!OSAtomicCompareAndSwapPtrBarrier(item, item->_next, (void **) &item->_prev->_next)) {
                PLCF_DEBUG("Failed to remove image list item despite holding lock");
            }
        }
        
        /* Now that the item is unreachable, update the prev/tail pointers. These are never accessed without a lock,
         * and need not be updated atomically. */
        if (item->_next != NULL) {
            /* Item is not the tail (otherwise next would be NULL), so simply update the next item's prev pointer. */
            item->_next->_prev = item->_prev;
        } else {
            /* Item is the tail (next is NULL). Simply update the tail record. */
            _tail = item->_prev;
        }
        
        /* If a reader is active, place the node on the free list. The item is unreachable here when readers
         * aren't active, so if we have a 0 refcount, we can safely delete the item, and be sure that no
         * reader holds a reference to it. */
        if (_refcount > 0) {
            item->_prev = NULL;
            item->_next = _free;
            
            if (_free != NULL)
                _free->_prev = item;
            _free = item;
        } else {
            delete item;
        }
    } OSSpinLockUnlock(&_write_lock);
}
CF_EXPORT CFNotificationCenterRef CFNotificationCenterGetDarwinNotifyCenter(void) {
    OSSpinLockLock(&centerLock);
    if (darwinCenter == NULL) {
        darwinCenter = _CFNotificationCenterCreate(kCFAllocatorDefault);
    }
    OSSpinLockUnlock(&centerLock);
    return darwinCenter;
}
示例#6
0
void BLI_spin_lock(SpinLock *spin)
{
#ifdef __APPLE__
	OSSpinLockLock(spin);
#else
	pthread_spin_lock(spin);
#endif
}
void CoreAudioOutput::setVolume(float vol)
{
	this->_volume = vol;
	
	OSSpinLockLock(this->_spinlockAU);
	AudioUnitSetParameter(this->_au, kHALOutputParam_Volume, kAudioUnitScope_Global, 0, vol, 0);
	OSSpinLockUnlock(this->_spinlockAU);
}
示例#8
0
        void DefSpinLock::Enter(const char* pFileName, int line)
        {
#if defined(_WIN32) || defined(_WIN64)
            // windows
#else
            OSSpinLockLock(&m_key);
#endif
        }
void CoreAudioOutput::stop()
{
	OSSpinLockLock(this->_spinlockAU);
	AudioOutputUnitStop(this->_au);
	OSSpinLockUnlock(this->_spinlockAU);
	
	this->clearBuffer();
}
示例#10
0
void CoreAudioOutput::start()
{
	this->clearBuffer();
	
	OSSpinLockLock(this->_spinlockAU);
	AudioUnitReset(this->_au, kAudioUnitScope_Global, 0);
	AudioOutputUnitStart(this->_au);
	OSSpinLockUnlock(this->_spinlockAU);
}
static void
_dispatch_introspection_thread_remove(void *ctxt)
{
	dispatch_introspection_thread_t dit = ctxt;
	OSSpinLockLock(&_dispatch_introspection_threads_lock);
	TAILQ_REMOVE(&_dispatch_introspection_threads, dit, dit_list);
	OSSpinLockUnlock(&_dispatch_introspection_threads_lock);
	_dispatch_continuation_free((void*)dit);
	_dispatch_thread_setspecific(dispatch_introspection_key, NULL);
}
示例#12
0
文件: gcd.c 项目: JosephKu/MacRuby
static VALUE
rb_dispatch_suspend(VALUE self, SEL sel)
{
    rb_dispatch_obj_t *dobj = RDispatch(self);
    OSSpinLockLock(&_suspensionLock);
    dobj->suspension_count++;
    OSSpinLockUnlock(&_suspensionLock);    
    dispatch_suspend(dobj->obj);
    return Qnil;
}
示例#13
0
void sighandler(int sig)
{
	if (sig == SIGUSR1) {
		OSSpinLockLock(&slock);
		OSSpinLockUnlock(&slock);		
	} else {
		// ALARM
		fprintf(stderr, "FAIL (%d)\n", i);
		exit(1);
	}
}
示例#14
0
文件: gcd.c 项目: JosephKu/MacRuby
static VALUE
rb_dispatch_resume(VALUE self, SEL sel)
{
    rb_dispatch_obj_t *dobj = RDispatch(self);
    OSSpinLockLock(&_suspensionLock);
    if (dobj->suspension_count > 0) {
        dobj->suspension_count--;
        dispatch_resume(dobj->obj);
    }
    OSSpinLockUnlock(&_suspensionLock);    
    return Qnil;
}
void
_dispatch_introspection_queue_dispose(dispatch_queue_t dq)
{
	DISPATCH_INTROSPECTION_INTERPOSABLE_HOOK_CALLOUT(queue_destroy, dq);
	if (DISPATCH_INTROSPECTION_HOOK_ENABLED(queue_dispose)) {
		_dispatch_introspection_queue_dispose_hook(dq);
	}

	OSSpinLockLock(&_dispatch_introspection_queues_lock);
	TAILQ_REMOVE(&_dispatch_introspection_queues, dq, diq_list);
	OSSpinLockUnlock(&_dispatch_introspection_queues_lock);
}
CF_EXPORT void CFNotificationCenterAddObserver(CFNotificationCenterRef center, const void *observer, CFNotificationCallback callBack, CFStringRef name, const void *object, CFNotificationSuspensionBehavior suspensionBehavior) {
    CFNotificationObserver *obs = (CFNotificationObserver *)malloc(sizeof(CFNotificationObserver));
    obs->retainCount = 0;
    obs->observer = observer;
    obs->callBack = callBack;
    obs->name = CFStringCreateCopy(kCFAllocatorDefault, name);
    obs->object = object;
    obs->suspensionBehavior = suspensionBehavior;
    OSSpinLockLock(&center->lock);
    CFArrayAppendValue(center->observers, obs);
    OSSpinLockUnlock(&center->lock);
}
示例#17
0
static CGColorSpaceRef CGColorSpaceCreateWithType(CGColorSpaceType type) {
    CGColorSpace* colorSpace;
    OSSpinLockLock(&CGColorSpaceCreateLock);
    {
        colorSpace=CGColorSpaces+type;
        if (!colorSpace->created) {
            _CGInitStaticObject(colorSpace,CGColorSpaceGetTypeID());
            colorSpace->created=true;
        }
    }
    OSSpinLockUnlock(&CGColorSpaceCreateLock);
    return colorSpace;
}
dispatch_queue_t
_dispatch_introspection_queue_create(dispatch_queue_t dq)
{
	OSSpinLockLock(&_dispatch_introspection_queues_lock);
	TAILQ_INSERT_TAIL(&_dispatch_introspection_queues, dq, diq_list);
	OSSpinLockUnlock(&_dispatch_introspection_queues_lock);

	DISPATCH_INTROSPECTION_INTERPOSABLE_HOOK_CALLOUT(queue_create, dq);
	if (DISPATCH_INTROSPECTION_HOOK_ENABLED(queue_create)) {
		_dispatch_introspection_queue_create_hook(dq);
	}
	return dq;
}
/**
 * Prepend a new entry value to the list
 *
 * @param value The value to be prepended.
 *
 * @warning This method is not async safe.
 */
template <typename V> void async_list<V>::nasync_prepend (V value) {
    /* Lock the list from other writers. */
    OSSpinLockLock(&_write_lock); {
        /* Construct the new entry, or recycle an existing one. */
        node *new_node;
        if (_free != NULL) {
            /* Fetch a node from the free list */
            new_node = _free;
            new_node->reset(value);
            
            /* Update the free list */
            _free = _free->_next;
        } else {
            new_node = new node(value);
        }
        
        /* Issue a memory barrier to ensure a consistent view of the value. */
        OSMemoryBarrier();
        
        /* If this is the first entry, initialize the list. */
        if (_tail == NULL) {
            
            /* Update the list tail. This need not be done atomically, as tail is never accessed by a lockless reader. */
            _tail = new_node;
            
            /* Atomically update the list head; this will be iterated upon by lockless readers. */
            if (!OSAtomicCompareAndSwapPtrBarrier(NULL, new_node, (void **) (&_head))) {
                /* Should never occur */
                PLCF_DEBUG("An async image head was set with tail == NULL despite holding lock.");
            }
        }
        
        /* Otherwise, prepend to the head of the list */
        else {
            new_node->_next = _head;
            new_node->_prev = NULL;
            
            /* Update the prev pointers. This is never accessed without a lock, so no additional synchronization
             * is required here. */
            _head->_prev = new_node;

            /* Issue a memory barrier to ensure a consistent view of the nodes. */
            OSMemoryBarrier();

            /* Atomically slot the new record into place; this may be iterated on by a lockless reader. */
            if (!OSAtomicCompareAndSwapPtrBarrier(new_node->_next, new_node, (void **) (&_head))) {
                PLCF_DEBUG("Failed to prepend to image list despite holding lock");
            }
        }
    } OSSpinLockUnlock(&_write_lock);
}
示例#20
0
void poc(){
  OSSpinLockLock(&lock);

  pthread_t t;
  pthread_create(&t, NULL, thread_func, NULL);


  mach_port_t conn = get_user_client("IntelAccelerator", 6);
  
  set_params(conn);
  OSSpinLockUnlock(&lock);
  IOServiceClose(conn);
  pthread_join(t, NULL);
}
CF_EXPORT void CFNotificationCenterRemoveObserver(CFNotificationCenterRef center, const void *observer, CFStringRef name, const void *object) {
    if (observer == NULL) {
        return;
    }
    OSSpinLockLock(&center->lock);
    CFNotificationObserver ctx = {
        .observer = observer,
        .name = name,
        .object = object,
        .context = center
    };
    struct __CFNotificationRemove notificationRemove = {
        .ctx = &ctx,
        .removed = 0,
        .more = 0
    };
    do {
        CFArrayApplyFunction(center->observers, CFRangeMake(0, CFArrayGetCount(center->observers)), (CFArrayApplierFunction)&removeObserver, &notificationRemove);
        for (int i=notificationRemove.removed-1; i >= 0; i--)
        {
            CFArrayRemoveValueAtIndex(center->observers, notificationRemove.removeIdx[i]);
        }
        if (notificationRemove.removed < __CFMaxRemove && !notificationRemove.more)
        {
            break;
        }
        notificationRemove.removed = 0;
        notificationRemove.more = 0;
    } while(1);
    OSSpinLockUnlock(&center->lock);
}

CF_EXPORT void CFNotificationCenterRemoveEveryObserver(CFNotificationCenterRef center, const void *observer) {
    OSSpinLockLock(&center->lock);
    CFArrayRemoveAllValues(center->observers);
    OSSpinLockUnlock(&center->lock);
}
示例#22
0
int main(int argc, char** argv){
  OSSpinLockLock(&lock);

  pthread_t t;
  pthread_create(&t, NULL, thread_func, NULL);

  mach_port_t conn = get_user_client("IOHDIXController", 0);
  
  set_params(conn);
  for(;;) {
    OSSpinLockUnlock(&lock);
    make_iokit_call();
  }
  return 0;
}
void
_dispatch_introspection_thread_add(void)
{
	if (_dispatch_thread_getspecific(dispatch_introspection_key)) {
		return;
	}
	uintptr_t thread = _dispatch_thread_self();
	dispatch_introspection_thread_t dit = (void*)_dispatch_continuation_alloc();
	dit->dit_isa = (void*)0x41;
	dit->thread = (void*)thread;
	dit->queue = !_dispatch_introspection_thread_queue_offset ? NULL :
			(void*)thread + _dispatch_introspection_thread_queue_offset;
	_dispatch_thread_setspecific(dispatch_introspection_key, dit);
	OSSpinLockLock(&_dispatch_introspection_threads_lock);
	TAILQ_INSERT_TAIL(&_dispatch_introspection_threads, dit, dit_list);
	OSSpinLockUnlock(&_dispatch_introspection_threads_lock);
}
示例#24
0
文件: gcd.c 项目: JosephKu/MacRuby
static void
rb_source_finalize(void *rcv, SEL sel)
{
    rb_source_t *src = RSource(rcv);
    if (src->source != NULL) {
        OSSpinLockLock(&_suspensionLock);    
        while (src->suspension_count > 0) {
            src->suspension_count--;
            dispatch_resume(src->source);
        }
        dispatch_release(src->source);
        OSSpinLockUnlock(&_suspensionLock);            
    }
    if (rb_source_finalize_super != NULL) {
        ((void(*)(void *, SEL))rb_source_finalize_super)(rcv, sel);
    }
}
CF_EXPORT void CFNotificationCenterPostNotificationWithOptions(CFNotificationCenterRef center, CFStringRef name, const void *object, CFDictionaryRef userInfo, CFOptionFlags options) {
    // since this is not cross process, we can just deliver all of the notifs immediately
    OSSpinLockLock(&center->lock);
    CFArrayRef observers = CFArrayCreateCopy(kCFAllocatorDefault, center->observers);
    OSSpinLockUnlock(&center->lock);
    
    CFIndex count = CFArrayGetCount(observers);
    for (CFIndex idx = 0; idx < count; idx++) {
        CFNotificationObserver *observer = (CFNotificationObserver *)CFArrayGetValueAtIndex(observers, idx);
        if (name == NULL || observer->name == NULL || CFStringCompare(observer->name, name, 0) == kCFCompareEqualTo) {
            if (object == NULL || observer->object == NULL || object == observer->object) {
                observer->callBack(center, (void *)observer->observer, name, object, userInfo);
            }
        }
    }
    
    CFRelease(observers);
}
示例#26
0
// Provides key info, using a cache to dramatically improve the energy impact of smcFanControl
kern_return_t SMCGetKeyInfo(io_connect_t conn, UInt32 key, SMCKeyData_keyInfo_t* keyInfo)
{
	SMCKeyData_t inputStructure;
	SMCKeyData_t outputStructure;
	kern_return_t result = kIOReturnSuccess;
	int i = 0;

	OSSpinLockLock(&g_keyInfoSpinLock);

	for (; i < g_keyInfoCacheCount; ++i)
	{
		if (key == g_keyInfoCache[i].key)
		{
			*keyInfo = g_keyInfoCache[i].keyInfo;
			break;
		}
	}

	if (i == g_keyInfoCacheCount)
	{
		// Not in cache, must look it up.
		memset(&inputStructure, 0, sizeof(inputStructure));
		memset(&outputStructure, 0, sizeof(outputStructure));

		inputStructure.key = key;
		inputStructure.data8 = SMC_CMD_READ_KEYINFO;

		result = SMCCall(conn, KERNEL_INDEX_SMC, &inputStructure, &outputStructure);
		if (result == kIOReturnSuccess)
		{
			*keyInfo = outputStructure.keyInfo;
			if (g_keyInfoCacheCount < KEY_INFO_CACHE_SIZE)
			{
				g_keyInfoCache[g_keyInfoCacheCount].key = key;
				g_keyInfoCache[g_keyInfoCacheCount].keyInfo = outputStructure.keyInfo;
				++g_keyInfoCacheCount;
			}
		}
	}

	OSSpinLockUnlock(&g_keyInfoSpinLock);

	return result;
}
示例#27
0
int main(int argc, const char *argv[])
{
	pthread_rwlock_init(&lock, NULL);
	signal(SIGUSR1, sighandler);
	signal(SIGALRM, sighandler);

	alarm(30);

	while (i++ < 10000) {
		pthread_rwlock_wrlock(&lock);
		pthread_create(&thr, NULL, thread, NULL);
		OSSpinLockLock(&slock);
		pthread_kill(thr, SIGUSR1);
		pthread_rwlock_unlock(&lock);
		OSSpinLockUnlock(&slock);
	}
	
	fprintf(stderr, "PASS\n");
	return 0;
}
示例#28
0
文件: gcd.c 项目: JosephKu/MacRuby
static void
rb_queue_finalize(void *rcv, SEL sel)
{
    rb_queue_t *queue = RQueue(rcv);
    if (queue->queue != NULL) 
    {
        OSSpinLockLock(&_suspensionLock);
        while (queue->suspension_count > 0) {
            queue->suspension_count--;
            dispatch_resume(queue->queue);
        }
        if (queue->should_release_queue) {
            dispatch_release(queue->queue);
            queue->should_release_queue = 0;
        }        
        OSSpinLockUnlock(&_suspensionLock);
    }
    if (rb_queue_finalize_super != NULL) {
        ((void(*)(void *, SEL))rb_queue_finalize_super)(rcv, sel);
    }
}
示例#29
0
int main(int argc, char** argv){
  kern_return_t err;
  // re map the null page rw
  int var = 0;
  err = vm_deallocate(mach_task_self(), 0x0, 0x1000);
  if (err != KERN_SUCCESS){
    printf("%x\n", err);
  }
  vm_address_t addr = 0;
  err = vm_allocate(mach_task_self(), &addr, 0x1000, 0);
  if (err != KERN_SUCCESS){
    if (err == KERN_INVALID_ADDRESS){
      printf("invalid address\n");
    }
    if (err == KERN_NO_SPACE){
      printf("no space\n");
    }
    printf("%x\n", err);
  }
  char* np = 0;
  for (int i = 0; i < 0x1000; i++){
    np[i] = '\xff';
  }

  *((uint64_t*)0x28) = 0xffffff4141414141;


  OSSpinLockLock(&lock);

  pthread_t t;
  pthread_create(&t, NULL, thread_func, NULL);


  mach_port_t conn = get_user_client("IOAudioEngine", 0);
  
  set_params(conn);
  OSSpinLockUnlock(&lock);
  IOServiceClose(conn);

}
示例#30
0
int main(int argc, char** argv){
  kern_return_t err;
  OSSpinLockLock(&lock);

  pthread_t t;
  pthread_create(&t, NULL, thread_func, NULL);

  mach_port_t service = IOServiceGetMatchingService(kIOMasterPortDefault,
                                                    IOServiceMatching("IOHDIXController"));

  mach_port_t conn = MACH_PORT_NULL;
  IOServiceOpen(service, mach_task_self(), 0, &conn);
  
  set_params(conn);
  for(;;) {
    OSSpinLockUnlock(&lock);
    CFTypeRef p = IORegistryEntryCreateCFProperty(service,
                                                  CFSTR("di-root-image-result"),
                                                  kCFAllocatorDefault,
                                                  0);
  }
  return 0;
}