[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 |
|
---|
[109] | 12 | CF_Mutex_Ctx globalMutex;
|
---|
[105] | 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 |
|
---|
[125] | 22 | if (CF_Mutex_Lock (globalMutex) < 0) { // for critical section
|
---|
[105] | 23 | // error
|
---|
| 24 | }
|
---|
| 25 | th = cnt++;
|
---|
[125] | 26 | if (CF_Mutex_Unlock (globalMutex) < 0) { // for critical section
|
---|
[105] | 27 | // error
|
---|
| 28 | }
|
---|
| 29 |
|
---|
| 30 | CF_DEBUG_PRINT (stderr, "created %dth thread\n", th);
|
---|
| 31 |
|
---|
[121] | 32 | for (i = 0 ; i < 10000 ; i++)
|
---|
[105] | 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 |
|
---|
[35] | 42 | int main (void)
|
---|
| 43 | {
|
---|
[109] | 44 | int i, j;
|
---|
| 45 | char idname[16] = {0x00,};
|
---|
| 46 | CF_Thread_Ctx tid[10];
|
---|
[35] | 47 |
|
---|
[105] | 48 | /* initialize */
|
---|
[108] | 49 | if (CF_Log_Initialize (10) < 0)
|
---|
[40] | 50 | {
|
---|
| 51 | fprintf (stderr, "failed to init. log\n");
|
---|
| 52 | return -1;
|
---|
| 53 | }
|
---|
[35] | 54 |
|
---|
| 55 | for (i = 0 ; i < 10 ; i++)
|
---|
| 56 | {
|
---|
[105] | 57 | sprintf (idname, "logid%d.txt", i);
|
---|
| 58 | if (CF_Log_Open (i, idname, CF_LOG_NO_BUFFER) < 0)
|
---|
[40] | 59 | fprintf (stderr, "failed to open log\n");
|
---|
[35] | 60 | }
|
---|
| 61 |
|
---|
| 62 | for (i = 0 ; i < 10 ; i++)
|
---|
| 63 | {
|
---|
| 64 | for (j = 0 ; j < 10000 ; j++)
|
---|
[40] | 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 | }
|
---|
[35] | 70 |
|
---|
[40] | 71 | if (CF_Log_Close (i) < 0)
|
---|
| 72 | fprintf (stderr, "failed to close log\n");
|
---|
[35] | 73 | }
|
---|
| 74 |
|
---|
[105] | 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 |
|
---|
[109] | 82 | if (CF_Mutex_CreateCtx (&globalMutex) < 0) {
|
---|
[105] | 83 | // error
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | for (i = 0 ; i < 10 ; i++)
|
---|
| 87 | {
|
---|
[122] | 88 | if (CF_Thread_CreateCtx (&tid[i], test_log_mt, &i) < 0)
|
---|
[105] | 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 | {
|
---|
[122] | 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)
|
---|
[105] | 107 | CF_DEBUG_PRINT (stderr, "failed to join %dth thread\n", i);
|
---|
[122] | 108 | if (CF_Thread_DestroyCtx (tid[i]) < 0)
|
---|
[105] | 109 | CF_DEBUG_PRINT (stderr, "failed to release %dth thread\n", i);
|
---|
| 110 | }
|
---|
| 111 |
|
---|
[122] | 112 | if (CF_Mutex_DestoryCtx (globalMutex) < 0) {
|
---|
[105] | 113 | // error
|
---|
| 114 | }
|
---|
| 115 | /* }}} mt */
|
---|
| 116 |
|
---|
| 117 | /* finalize */
|
---|
[35] | 118 | CF_Log_Finalize ();
|
---|
| 119 |
|
---|
| 120 | return 0;
|
---|
| 121 | }
|
---|