1 | /**
|
---|
2 | * @file log.c
|
---|
3 | * @author myusgun <myusgun@gmail.com>
|
---|
4 | */
|
---|
5 | #include "cf_log.h"
|
---|
6 | #include "cf_mutex.h"
|
---|
7 | #include "cf_thread.h"
|
---|
8 | #include "cf_debug.h"
|
---|
9 |
|
---|
10 | #include <stdio.h>
|
---|
11 |
|
---|
12 | CF_Mutex_Ctx globalMutex;
|
---|
13 |
|
---|
14 | #define LOG_MT 1
|
---|
15 |
|
---|
16 | int test_log_mt (void * arg)
|
---|
17 | {
|
---|
18 | static int cnt = 0;
|
---|
19 | int i = 0;
|
---|
20 | int th = 0;
|
---|
21 |
|
---|
22 | if (CF_Mutex_Lock (globalMutex) < 0) { // for critical section
|
---|
23 | // error
|
---|
24 | }
|
---|
25 | th = cnt++;
|
---|
26 | if (CF_Mutex_Unlock (globalMutex) < 0) { // for critical section
|
---|
27 | // error
|
---|
28 | }
|
---|
29 |
|
---|
30 | CF_DEBUG_PRINT (stderr, "created %dth thread\n", th);
|
---|
31 |
|
---|
32 | for (i = 0 ; i < 10000 ; i++)
|
---|
33 | {
|
---|
34 | CF_Log_Write (LOG_MT, "LOG_MT", "[%d] multi-threadedlogging test %d\n", th, i);
|
---|
35 | }
|
---|
36 |
|
---|
37 | CF_DEBUG_PRINT (stderr, "end %dth thread\n", th);
|
---|
38 |
|
---|
39 | return 0;
|
---|
40 | }
|
---|
41 |
|
---|
42 | int main (void)
|
---|
43 | {
|
---|
44 | int i, j;
|
---|
45 | char idname[16] = {0x00,};
|
---|
46 | CF_Thread_Ctx tid[10];
|
---|
47 |
|
---|
48 | /* initialize */
|
---|
49 | if (CF_Log_Initialize (10) < 0)
|
---|
50 | {
|
---|
51 | fprintf (stderr, "failed to init. log\n");
|
---|
52 | return -1;
|
---|
53 | }
|
---|
54 |
|
---|
55 | for (i = 0 ; i < 10 ; i++)
|
---|
56 | {
|
---|
57 | sprintf (idname, "logid%d.txt", i);
|
---|
58 | if (CF_Log_Open (i, idname, CF_LOG_NO_BUFFER) < 0)
|
---|
59 | fprintf (stderr, "failed to open log\n");
|
---|
60 | }
|
---|
61 |
|
---|
62 | for (i = 0 ; i < 10 ; i++)
|
---|
63 | {
|
---|
64 | for (j = 0 ; j < 10000 ; j++)
|
---|
65 | {
|
---|
66 | int result = CF_Log_Write (i, "LOG_ID_TEST", "turn %d\n", j);
|
---|
67 | if (result < 0)
|
---|
68 | fprintf (stderr, "failed to write log %d\n", result);
|
---|
69 | }
|
---|
70 |
|
---|
71 | if (CF_Log_Close (i) < 0)
|
---|
72 | fprintf (stderr, "failed to close log\n");
|
---|
73 | }
|
---|
74 |
|
---|
75 | /* mt {{{ */
|
---|
76 | if (CF_Log_Open (LOG_MT, "log_mt.txt", CF_LOG_DEFAULT_BUFFER) < 0)
|
---|
77 | CF_DEBUG_PRINT (stderr, "create log ctx error\n");
|
---|
78 |
|
---|
79 | if (CF_Log_SetMT (LOG_MT, CF_TRUE) < 0)
|
---|
80 | CF_DEBUG_PRINT (stderr, "set multi-threading mode error\n");
|
---|
81 |
|
---|
82 | if (CF_Mutex_CreateCtx (&globalMutex) < 0) {
|
---|
83 | // error
|
---|
84 | }
|
---|
85 |
|
---|
86 | for (i = 0 ; i < 10 ; i++)
|
---|
87 | {
|
---|
88 | if (CF_Thread_CreateCtx (&tid[i], test_log_mt, &i) < 0)
|
---|
89 | {
|
---|
90 | CF_DEBUG_PRINT (stderr, "failed to create %dth thread\n", i);
|
---|
91 | return -2;
|
---|
92 | }
|
---|
93 | }
|
---|
94 |
|
---|
95 | for (i = 0 ; i < 10 ; i++)
|
---|
96 | {
|
---|
97 | if (CF_Thread_Start (tid[i]) < 0)
|
---|
98 | {
|
---|
99 | CF_DEBUG_PRINT (stderr, "failed to start %dth thread\n", i);
|
---|
100 | return -3;
|
---|
101 | }
|
---|
102 | }
|
---|
103 |
|
---|
104 | for (i = 0 ; i < 10 ; i++)
|
---|
105 | {
|
---|
106 | if (CF_Thread_Join (tid[i]) < 0)
|
---|
107 | CF_DEBUG_PRINT (stderr, "failed to join %dth thread\n", i);
|
---|
108 | if (CF_Thread_DestroyCtx (tid[i]) < 0)
|
---|
109 | CF_DEBUG_PRINT (stderr, "failed to release %dth thread\n", i);
|
---|
110 | }
|
---|
111 |
|
---|
112 | if (CF_Mutex_DestoryCtx (globalMutex) < 0) {
|
---|
113 | // error
|
---|
114 | }
|
---|
115 | /* }}} mt */
|
---|
116 |
|
---|
117 | /* finalize */
|
---|
118 | CF_Log_Finalize ();
|
---|
119 |
|
---|
120 | return 0;
|
---|
121 | }
|
---|