Skip to content

Instantly share code, notes, and snippets.

@jpbland1
Created January 16, 2023 20:54
Show Gist options
  • Save jpbland1/f3eeb83c0d413152281e5058246d3e55 to your computer and use it in GitHub Desktop.
Save jpbland1/f3eeb83c0d413152281e5058246d3e55 to your computer and use it in GitHub Desktop.
#include <wolftpm/tpm2.h>
#include <wolftpm/tpm2_wrap.h>
#include <stdio.h>
#include <pthread.h>
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
void workThread(void* args)
{
int ret;
TPM2_CTX tpm2Ctx;
ret = TPM2_Init(&tpm2Ctx, NULL, NULL);
/* lock so that the other thread must wait while we set the ctx */
pthread_mutex_lock(&mutex);
/* ctx should be what was set in init, not set by other thread */
if (TPM2_GetActiveCtx() != &tpm2Ctx)
printf("ERROR ctx mismatch %p\n", TPM2_GetActiveCtx());
else
printf("ctx match\n");
/* set the active ctx, should not impact the other thread */
TPM2_SetActiveCtx(&tpm2Ctx);
/* let the other thread run */
pthread_mutex_unlock(&mutex);
(void)args;
}
int main(void)
{
pthread_t thread_1;
pthread_t thread_2;
pthread_create(&thread_1, NULL, &workThread, NULL);
pthread_create(&thread_2, NULL, &workThread, NULL);
pthread_join(thread_1, NULL);
pthread_join(thread_2, NULL);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment