File: main.c

package info (click to toggle)
libkqueue 0.9.2-2
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 508 kB
  • ctags: 614
  • sloc: ansic: 5,418; sh: 258; makefile: 202; perl: 22
file content (99 lines) | stat: -rw-r--r-- 2,298 bytes parent folder | download | duplicates (3)
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
 * Copyright (c) 2009 Mark Heily <mark@heily.com>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include <pthread.h> 
#include <sys/event.h> 
#include <dispatch/dispatch.h> 

pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
int testnum;

void test_countdown(void);

void
say_hello(void *arg)
{
	puts("hello");
	test_countdown();
}

void
final_countdown(void *arg, size_t count)
{
	static int europe = 10;

	if (europe == 0) {
		printf("It's the final countdown..\n");
		exit(0);
	} else {
		printf("%d.. ", europe);
		fflush(stdout);
	}

	pthread_mutex_lock(&mtx);
	europe--;
	pthread_mutex_unlock(&mtx);
}

/* Adapted from:
     http://developer.apple.com/mac/articles/cocoa/introblocksgcd.html
*/
void
test_timer()
{
	dispatch_source_t timer;
	dispatch_time_t now;

    timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, 
			dispatch_get_current_queue()); //NOTE: q_default doesn't work
   	now = dispatch_walltime(DISPATCH_TIME_NOW, 0);
	dispatch_source_set_timer(timer, now, 1, 1);
	dispatch_source_set_event_handler_f(timer, say_hello);
	puts("starting timer\n");
}

void
test_countdown(void)
{
	dispatch_apply_f(15,
			dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0),
			NULL, final_countdown);
}


int 
main(int argc, char **argv)
{
    while (argc) {
#if TODO
        if (strcmp(argv[0], "--no-proc") == 0)
            test_proc = 0;
#endif
        argv++;
        argc--;
    }

	test_timer();

	dispatch_main();
    printf("\n---\n"
            "+OK All %d tests completed.\n", testnum - 1);
    return (0);
}