1
0
Fork 0
mirror of https://github.com/ossrs/srs.git synced 2025-02-13 11:51:57 +00:00
srs/trunk/research/st/srs.c

126 lines
2.6 KiB
C
Raw Normal View History

2014-10-26 05:43:11 +00:00
#include <unistd.h>
#include <stdio.h>
2014-10-18 05:16:17 +00:00
#include "public.h"
2014-10-26 05:43:11 +00:00
#define srs_trace(msg, ...) printf(msg, ##__VA_ARGS__);printf("\n")
2014-10-26 08:35:12 +00:00
st_mutex_t sync_start = NULL;
st_cond_t sync_cond = NULL;
st_mutex_t sync_mutex = NULL;
2014-10-26 08:38:00 +00:00
st_cond_t sync_end = NULL;
2014-10-26 08:35:12 +00:00
void* sync_master(void* arg)
2014-10-26 05:43:11 +00:00
{
2014-10-26 08:35:12 +00:00
// wait for main to sync_start this thread.
st_mutex_lock(sync_start);
st_mutex_unlock(sync_start);
st_usleep(100 * 1000);
2014-10-26 08:35:12 +00:00
st_cond_signal(sync_cond);
2014-10-26 08:35:12 +00:00
st_mutex_lock(sync_mutex);
srs_trace("2. st mutex is ok");
2014-10-26 08:35:12 +00:00
st_mutex_unlock(sync_mutex);
st_usleep(100 * 1000);
srs_trace("3. st thread is ok");
2014-10-26 08:35:12 +00:00
st_cond_signal(sync_cond);
2014-10-26 05:43:11 +00:00
return NULL;
}
2014-10-26 08:35:12 +00:00
void* sync_slave(void* arg)
{
// lock mutex to control thread.
2014-10-26 08:35:12 +00:00
st_mutex_lock(sync_mutex);
2014-10-26 08:35:12 +00:00
// wait for main to sync_start this thread.
st_mutex_lock(sync_start);
st_mutex_unlock(sync_start);
// wait thread to ready.
2014-10-26 08:35:12 +00:00
st_cond_wait(sync_cond);
srs_trace("1. st cond is ok");
// release mutex to control thread
st_usleep(100 * 1000);
2014-10-26 08:35:12 +00:00
st_mutex_unlock(sync_mutex);
// wait thread to exit.
2014-10-26 08:35:12 +00:00
st_cond_wait(sync_cond);
srs_trace("4. st is ok");
2014-10-26 08:38:00 +00:00
st_cond_signal(sync_end);
return NULL;
}
2014-10-26 08:35:12 +00:00
int sync_test()
2014-10-18 05:16:17 +00:00
{
2014-10-26 08:38:00 +00:00
srs_trace("sync test: start");
2014-10-26 08:35:12 +00:00
if ((sync_start = st_mutex_new()) == NULL) {
srs_trace("st_mutex_new sync_start failed");
2014-10-26 05:43:11 +00:00
return -1;
}
2014-10-26 08:35:12 +00:00
st_mutex_lock(sync_start);
2014-10-26 08:35:12 +00:00
if ((sync_cond = st_cond_new()) == NULL) {
srs_trace("st_cond_new cond failed");
return -1;
}
2014-10-26 08:38:00 +00:00
if ((sync_end = st_cond_new()) == NULL) {
srs_trace("st_cond_new end failed");
return -1;
}
2014-10-26 08:35:12 +00:00
if ((sync_mutex = st_mutex_new()) == NULL) {
srs_trace("st_mutex_new mutex failed");
return -1;
}
2014-10-26 05:43:11 +00:00
2014-10-26 08:35:12 +00:00
if (!st_thread_create(sync_master, NULL, 0, 0)) {
2014-10-26 05:43:11 +00:00
srs_trace("st_thread_create failed");
return -1;
}
2014-10-26 08:35:12 +00:00
if (!st_thread_create(sync_slave, NULL, 0, 0)) {
srs_trace("st_thread_create failed");
return -1;
}
// run all threads.
2014-10-26 08:35:12 +00:00
st_mutex_unlock(sync_start);
2014-10-26 08:38:00 +00:00
st_cond_wait(sync_end);
srs_trace("sync test: end");
2014-10-26 08:35:12 +00:00
return 0;
}
int main(int argc, char** argv)
{
if (st_set_eventsys(ST_EVENTSYS_ALT) < 0) {
srs_trace("st_set_eventsys failed");
return -1;
}
if (st_init() < 0) {
srs_trace("st_init failed");
return -1;
}
if (sync_test() < 0) {
srs_trace("sync_test failed");
return -1;
}
2014-10-26 05:43:11 +00:00
// cleanup.
st_thread_exit(NULL);
2014-10-18 05:16:17 +00:00
return 0;
}