|
| 1 | +/**************************************************************************** |
| 2 | + * apps/testing/ostest/spinlock.c |
| 3 | + * |
| 4 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 5 | + * contributor license agreements. See the NOTICE file distributed with |
| 6 | + * this work for additional information regarding copyright ownership. The |
| 7 | + * ASF licenses this file to you under the Apache License, Version 2.0 (the |
| 8 | + * "License"); you may not use this file except in compliance with the |
| 9 | + * License. You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 15 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 16 | + * License for the specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + * |
| 19 | + ****************************************************************************/ |
| 20 | + |
| 21 | +/**************************************************************************** |
| 22 | + * Included Files |
| 23 | + ****************************************************************************/ |
| 24 | + |
| 25 | +#include <stdio.h> |
| 26 | +#include <assert.h> |
| 27 | +#include <errno.h> |
| 28 | +#include <nuttx/spinlock.h> |
| 29 | + |
| 30 | +#include "ostest.h" |
| 31 | + |
| 32 | +/**************************************************************************** |
| 33 | + * Pre-processor Definitions |
| 34 | + ****************************************************************************/ |
| 35 | + |
| 36 | +/**************************************************************************** |
| 37 | + * Private Data |
| 38 | + ****************************************************************************/ |
| 39 | + |
| 40 | +static spinlock_t lock = SP_UNLOCKED; |
| 41 | + |
| 42 | +static pthread_t g_thread1; |
| 43 | +static pthread_t g_thread2; |
| 44 | + |
| 45 | +static int g_result = 0; |
| 46 | + |
| 47 | +static FAR void thread_spinlock(FAR void *parameter) |
| 48 | +{ |
| 49 | + int pid = *(int *)parameter; |
| 50 | + |
| 51 | + for (int i = 0; i < 10; i++) |
| 52 | + { |
| 53 | + printf("pid %d get lock g_result:%d\n", pid, g_result); |
| 54 | + spin_lock(&lock); |
| 55 | + g_result++; |
| 56 | + spin_unlock(&lock); |
| 57 | + printf("pid %d release lock g_result:%d\n", pid, g_result); |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +/**************************************************************************** |
| 62 | + * Public Functions |
| 63 | + ****************************************************************************/ |
| 64 | + |
| 65 | +void spinlock_test(void) |
| 66 | +{ |
| 67 | + int status; |
| 68 | + g_result = 0; |
| 69 | + |
| 70 | + status = pthread_create(&g_thread1, NULL, |
| 71 | + (void *)thread_spinlock, &g_thread1); |
| 72 | + if (status != 0) |
| 73 | + { |
| 74 | + printf("spinlock_test: ERROR pthread_create failed, status=%d\n", |
| 75 | + status); |
| 76 | + ASSERT(false); |
| 77 | + } |
| 78 | + |
| 79 | + status = pthread_create(&g_thread2, NULL, |
| 80 | + (void *)thread_spinlock, &g_thread2); |
| 81 | + if (status != 0) |
| 82 | + { |
| 83 | + printf("spinlock_test: ERROR pthread_create failed, status=%d\n", |
| 84 | + status); |
| 85 | + ASSERT(false); |
| 86 | + } |
| 87 | + |
| 88 | + pthread_join(g_thread1, NULL); |
| 89 | + pthread_join(g_thread2, NULL); |
| 90 | + |
| 91 | + assert(g_result == 20); |
| 92 | +} |
0 commit comments