summaryrefslogtreecommitdiff
path: root/examples/libc/sem.c
blob: d081d246e93eee93c39ce1205239909dbc326589 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <pthread.h>
#include <semaphore.h>
#include <stdio.h>

static sem_t sema;
static void* other_thread()
{
	printf("other_thread here!\n");

	sleep(1);

	while (1)
	{
		printf("other_thread: sem_post...\n");
		if(sem_post(&sema) == -1)
			printf("sem_post failed\n");
		sleep(1);
	}
	
	printf("other_thread dies!\n");
	pthread_exit(0);
}

static void test_thread(void* parameter)
{
	pthread_t tid;

	printf("main thread here!\n");
	printf("sleep 5 seconds...");	
	sleep(5);
	printf("done\n");

	sem_init(&sema, 0, 0);
	
	/* create the "other" thread */
	if(pthread_create(&tid, 0, &other_thread, 0)!=0)
		/* error */
		printf("pthread_create OtherThread failed.\n");
	else
		printf("created OtherThread=%x\n", tid);

	/* let the other thread run */
	while (1)
	{
		printf("Main: sem_wait...\n");
		if(sem_wait(&sema) == -1)
			printf("sem_wait failed\n");
		printf("Main back.\n\n");
	}

	pthread_exit(0);
}
#include <finsh.h>
void libc_sem()
{
	rt_thread_t tid;

	tid = rt_thread_create("semtest", test_thread, RT_NULL, 
		2048, 20, 5);
	if (tid != RT_NULL)
	{
		rt_thread_startup(tid);
	}
}
FINSH_FUNCTION_EXPORT(libc_sem, posix semaphore test);