xref: /aosp_15_r20/external/sandboxed-api/sandboxed_api/sandbox2/testcases/tsync.cc (revision ec63e07ab9515d95e79c211197c445ef84cefa6a)
1 // Copyright 2019 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 // A binary that starts a thread then calls SandboxMeHere.
16 // It is used to test tsync support.
17 
18 #include <pthread.h>
19 #include <unistd.h>
20 
21 #include <cstdio>
22 #include <cstdlib>
23 
24 #include "sandboxed_api/sandbox2/client.h"
25 #include "sandboxed_api/sandbox2/comms.h"
26 
27 static pthread_barrier_t g_barrier;
28 
Sleepy(void *)29 void* Sleepy(void*) {
30   pthread_barrier_wait(&g_barrier);
31   while (true) {
32     printf("hello from thread\n");
33     sleep(1);
34   }
35 }
36 
main(int argc,char * argv[])37 int main(int argc, char* argv[]) {
38   pthread_t thread;
39 
40   if (pthread_barrier_init(&g_barrier, nullptr, 2) < 0) {
41     fprintf(stderr, "pthread_barrier_init: error\n");
42     return EXIT_FAILURE;
43   }
44 
45   if (pthread_create(&thread, nullptr, Sleepy, nullptr)) {
46     fprintf(stderr, "pthread_create: error\n");
47     return EXIT_FAILURE;
48   }
49 
50   printf("hello from main\n");
51 
52   // Wait to make sure that the sleepy-thread is up and running.
53   pthread_barrier_wait(&g_barrier);
54 
55   sandbox2::Comms comms(sandbox2::Comms::kDefaultConnection);
56   sandbox2::Client sandbox2_client(&comms);
57   sandbox2_client.SandboxMeHere();
58 
59   return EXIT_SUCCESS;
60 }
61