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