2012-10-25 22:06:46 +02:00
|
|
|
/****************************************************************************
|
2021-06-16 09:22:16 +02:00
|
|
|
* apps/examples/elf/tests/pthread/pthread.c
|
2012-10-25 22:06:46 +02:00
|
|
|
*
|
2021-06-15 09:09:58 +02:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2012-10-25 22:06:46 +02:00
|
|
|
*
|
2021-06-15 09:09:58 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2012-10-25 22:06:46 +02:00
|
|
|
*
|
2021-06-15 09:09:58 +02:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2012-10-25 22:06:46 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <pthread.h>
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#define CHILD_ARG ((void*)0x12345678)
|
|
|
|
#define CHILD_RET ((void*)0x87654321)
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
enum exit_values_e
|
|
|
|
{
|
|
|
|
TESTRESULT_SUCCESS = 0,
|
|
|
|
TESTRESULT_PTHREAD_ATTR_INIT_FAIL,
|
|
|
|
TESTRESULT_PTHREAD_CREATE_FAIL,
|
|
|
|
TESTRESULT_PTHREAD_JOIN_FAIL,
|
|
|
|
TESTRESULT_CHILD_ARG_FAIL,
|
|
|
|
TESTRESULT_CHILD_RETVAL_FAIL,
|
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* External Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-02-15 16:50:18 +01:00
|
|
|
/* NOTE: it is necessary for functions that are referred to by function
|
|
|
|
* pointers pointer to be declared with global scope (at least for ARM).
|
|
|
|
* Otherwise, a relocation type that is not supported by ELF is generated
|
|
|
|
* by GCC.
|
2012-10-25 22:06:46 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
void *child_start_routine(void *arg)
|
|
|
|
{
|
2020-02-15 16:50:18 +01:00
|
|
|
printf("CHILD: started with arg=%d\n", (int)((intptr_t)arg));
|
2012-10-25 22:06:46 +02:00
|
|
|
|
|
|
|
if (arg != CHILD_ARG)
|
|
|
|
{
|
2020-02-15 16:50:18 +01:00
|
|
|
printf("CHILD: expected arg=%d\n", (int)((intptr_t)CHILD_ARG));
|
2012-10-25 22:06:46 +02:00
|
|
|
return (void*)TESTRESULT_CHILD_ARG_FAIL;
|
|
|
|
}
|
2020-02-15 16:50:18 +01:00
|
|
|
|
2012-10-25 22:06:46 +02:00
|
|
|
sleep(2);
|
|
|
|
|
2020-02-15 16:50:18 +01:00
|
|
|
printf("CHILD: returning %d\n", (int)((intptr_t)CHILD_RET));
|
2012-10-25 22:06:46 +02:00
|
|
|
pthread_exit(CHILD_RET);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
pthread_attr_t attr;
|
|
|
|
pthread_t thread;
|
|
|
|
void *retval;
|
|
|
|
int status;
|
|
|
|
|
|
|
|
puts("PARENT: started\n");
|
|
|
|
|
|
|
|
status = pthread_attr_init(&attr);
|
|
|
|
if (status != 0)
|
|
|
|
{
|
|
|
|
printf("PARENT: pthread_attr_init() returned %d\n", status);
|
|
|
|
exit(TESTRESULT_PTHREAD_ATTR_INIT_FAIL);
|
|
|
|
}
|
|
|
|
|
2020-02-15 16:50:18 +01:00
|
|
|
printf("PARENT: calling pthread_start with arg=%d\n",
|
|
|
|
(int)((intptr_t)CHILD_ARG));
|
|
|
|
|
2012-10-25 22:06:46 +02:00
|
|
|
status = pthread_create(&thread, &attr, child_start_routine, CHILD_ARG);
|
|
|
|
if (status != 0)
|
|
|
|
{
|
|
|
|
printf("PARENT: pthread_create() returned %d\n", status);
|
|
|
|
exit(TESTRESULT_PTHREAD_CREATE_FAIL);
|
|
|
|
}
|
|
|
|
|
|
|
|
status = pthread_join(thread, &retval);
|
|
|
|
if (status != 0)
|
|
|
|
{
|
|
|
|
printf("PARENT pthread_join() returned %d\n", status);
|
|
|
|
|
|
|
|
exit(TESTRESULT_PTHREAD_JOIN_FAIL);
|
|
|
|
}
|
|
|
|
|
2020-02-15 16:50:18 +01:00
|
|
|
printf("PARENT child exitted with %d\n", (int)((intptr_t)retval));
|
2012-10-25 22:06:46 +02:00
|
|
|
if (retval != CHILD_RET)
|
|
|
|
{
|
2020-02-15 16:50:18 +01:00
|
|
|
printf("PARENT child thread did not exit with %d\n",
|
|
|
|
(int)((intptr_t)CHILD_RET));
|
|
|
|
|
2012-10-25 22:06:46 +02:00
|
|
|
exit(TESTRESULT_CHILD_RETVAL_FAIL);
|
|
|
|
}
|
|
|
|
|
|
|
|
puts("PARENT returning success\n");
|
|
|
|
return TESTRESULT_SUCCESS;
|
|
|
|
}
|