FreeRDP
TestPoolWork.c
1 
2 #include <winpr/wtypes.h>
3 #include <winpr/crt.h>
4 #include <winpr/pool.h>
5 #include <winpr/interlocked.h>
6 
7 static LONG count = 0;
8 
9 static void CALLBACK test_WorkCallback(PTP_CALLBACK_INSTANCE instance, void* context, PTP_WORK work)
10 {
11  printf("Hello %s: %03" PRId32 " (thread: 0x%08" PRIX32 ")\n", (char*)context,
12  InterlockedIncrement(&count), GetCurrentThreadId());
13 
14  for (int index = 0; index < 100; index++)
15  {
16  BYTE a[1024];
17  BYTE b[1024];
18  BYTE c[1024] = { 0 };
19 
20  FillMemory(a, ARRAYSIZE(a), 0xAA);
21  FillMemory(b, ARRAYSIZE(b), 0xBB);
22 
23  CopyMemory(c, a, ARRAYSIZE(a));
24  CopyMemory(c, b, ARRAYSIZE(b));
25  }
26 }
27 
28 static BOOL test1(void)
29 {
30  PTP_WORK work = NULL;
31  printf("Global Thread Pool\n");
32  work = CreateThreadpoolWork(test_WorkCallback, "world", NULL);
33 
34  if (!work)
35  {
36  printf("CreateThreadpoolWork failure\n");
37  return FALSE;
38  }
39 
46  for (int index = 0; index < 10; index++)
47  SubmitThreadpoolWork(work);
48 
49  WaitForThreadpoolWorkCallbacks(work, FALSE);
50  CloseThreadpoolWork(work);
51  return TRUE;
52 }
53 
54 static BOOL test2(void)
55 {
56  BOOL rc = FALSE;
57  PTP_POOL pool = NULL;
58  PTP_WORK work = NULL;
59  PTP_CLEANUP_GROUP cleanupGroup = NULL;
60  TP_CALLBACK_ENVIRON environment;
61  printf("Private Thread Pool\n");
62 
63  if (!(pool = CreateThreadpool(NULL)))
64  {
65  printf("CreateThreadpool failure\n");
66  return FALSE;
67  }
68 
69  if (!SetThreadpoolThreadMinimum(pool, 4))
70  {
71  printf("SetThreadpoolThreadMinimum failure\n");
72  goto fail;
73  }
74 
75  SetThreadpoolThreadMaximum(pool, 8);
76  InitializeThreadpoolEnvironment(&environment);
77  SetThreadpoolCallbackPool(&environment, pool);
78  cleanupGroup = CreateThreadpoolCleanupGroup();
79 
80  if (!cleanupGroup)
81  {
82  printf("CreateThreadpoolCleanupGroup failure\n");
83  goto fail;
84  }
85 
86  SetThreadpoolCallbackCleanupGroup(&environment, cleanupGroup, NULL);
87  work = CreateThreadpoolWork(test_WorkCallback, "world", &environment);
88 
89  if (!work)
90  {
91  printf("CreateThreadpoolWork failure\n");
92  goto fail;
93  }
94 
95  for (int index = 0; index < 10; index++)
96  SubmitThreadpoolWork(work);
97 
98  WaitForThreadpoolWorkCallbacks(work, FALSE);
99  rc = TRUE;
100 fail:
101 
102  if (cleanupGroup)
103  {
104  CloseThreadpoolCleanupGroupMembers(cleanupGroup, TRUE, NULL);
105  CloseThreadpoolCleanupGroup(cleanupGroup);
106  DestroyThreadpoolEnvironment(&environment);
114 #if 0
115  CloseThreadpoolWork(work); // this would segfault, see comment above. */
116 #endif
117  }
118 
119  CloseThreadpool(pool);
120  return rc;
121 }
122 
123 int TestPoolWork(int argc, char* argv[])
124 {
125 
126  WINPR_UNUSED(argc);
127  WINPR_UNUSED(argv);
128 
129  if (!test1())
130  return -1;
131 
132  if (!test2())
133  return -1;
134 
135  return 0;
136 }