83 lines
2.7 KiB
Objective-C
83 lines
2.7 KiB
Objective-C
/*
|
|
* Author: Landon Fuller <landonf@plausiblelabs.com>
|
|
*
|
|
* Copyright (c) 2008-2013 Plausible Labs Cooperative, Inc.
|
|
* All rights reserved.
|
|
*
|
|
* Permission is hereby granted, free of charge, to any person
|
|
* obtaining a copy of this software and associated documentation
|
|
* files (the "Software"), to deal in the Software without
|
|
* restriction, including without limitation the rights to use,
|
|
* copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
* copies of the Software, and to permit persons to whom the
|
|
* Software is furnished to do so, subject to the following
|
|
* conditions:
|
|
*
|
|
* The above copyright notice and this permission notice shall be
|
|
* included in all copies or substantial portions of the Software.
|
|
*
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
|
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
* OTHER DEALINGS IN THE SOFTWARE.
|
|
*/
|
|
|
|
#include "PLCrashTestThread.h"
|
|
|
|
/**
|
|
* @internal
|
|
* @ingroup plcrash_internal
|
|
* @defgroup plcrash_test_thread Test Thread API
|
|
*
|
|
* An API capable of starting/stopping threads, to provide a simple stack to iterate.
|
|
* @{
|
|
*/
|
|
|
|
/* Thread entry point; simply waits to be asked to exit. */
|
|
static void *test_thread_entry (void *arg) {
|
|
plcrash_test_thread_t *args = arg;
|
|
|
|
/* Acquire the lock and inform our caller that we're active */
|
|
pthread_mutex_lock(&args->lock);
|
|
pthread_cond_signal(&args->cond);
|
|
|
|
/* Wait for a shut down request, and then drop the acquired lock immediately */
|
|
pthread_cond_wait(&args->cond, &args->lock);
|
|
pthread_mutex_unlock(&args->lock);
|
|
|
|
return NULL;
|
|
}
|
|
|
|
|
|
/** Spawn a test thread that may be used as an iterable stack. (For testing only!) */
|
|
void plcrash_test_thread_spawn (plcrash_test_thread_t *args) {
|
|
/* Initialize the args */
|
|
pthread_mutex_init(&args->lock, NULL);
|
|
pthread_cond_init(&args->cond, NULL);
|
|
|
|
/* Lock and start the thread */
|
|
pthread_mutex_lock(&args->lock);
|
|
pthread_create(&args->thread, NULL, test_thread_entry, args);
|
|
pthread_cond_wait(&args->cond, &args->lock);
|
|
pthread_mutex_unlock(&args->lock);
|
|
}
|
|
|
|
/** Stop a test thread. */
|
|
void plcrash_test_thread_stop (plcrash_test_thread_t *args) {
|
|
/* Signal the thread to exit */
|
|
pthread_mutex_lock(&args->lock);
|
|
pthread_cond_signal(&args->cond);
|
|
pthread_mutex_unlock(&args->lock);
|
|
|
|
/* Wait for exit */
|
|
pthread_join(args->thread, NULL);
|
|
}
|
|
|
|
/*
|
|
* @}
|
|
*/
|