Add Pthreads C example/test

This commit is contained in:
Shahram Najm 2016-05-24 14:27:46 +01:00
parent 68257583d8
commit 7a4a3d12d0
3 changed files with 113 additions and 12 deletions

View File

@ -6,7 +6,7 @@ configure_file( include.ctest.sh.in include.ctest.sh @ONLY )
execute_process( COMMAND ${CMAKE_COMMAND} -E copy_if_different ${CMAKE_CURRENT_SOURCE_DIR}/include.sh ${CMAKE_CURRENT_BINARY_DIR} )
# Build the executables used by test scripts
################################################
########################################################################
list( APPEND test_bins
grib_nearest
grib_set_bitmap
@ -57,7 +57,7 @@ foreach( tool ${test_bins} )
endforeach()
# Now add each test (shell scripts)
#################################################
########################################################################
list( APPEND tests
grib_iterator
grib_get_keys
@ -92,19 +92,34 @@ list( APPEND tests
)
foreach( test ${tests} )
ecbuild_add_test( TARGET eccodes_c_${test}
TYPE SCRIPT
COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/${test}.sh
TEST_DEPENDS eccodes_download_gribs eccodes_download_bufrs
TYPE SCRIPT
COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/${test}.sh
TEST_DEPENDS eccodes_download_gribs eccodes_download_bufrs
)
endforeach()
########################################################################
# Tests which are conditional
if( ENABLE_EXTRA_TESTS AND HAVE_ECCODES_THREADS )
# This one only for POSIX threads
ecbuild_add_executable( TARGET c_grib_pthreads
NOINSTALL
SOURCES grib_pthreads.c
LIBS eccodes
)
ecbuild_add_test( TARGET eccodes_c_grib_pthreads
TYPE SCRIPT
COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/grib_pthreads.sh
)
endif()
#############################################
########################################################################
# Tests with no script
ecbuild_add_test( TARGET eccodes_c_new_sample
SOURCES new_sample.c
LIBS eccodes
ARGS "out.grib"
ENVIRONMENT "ECCODES_SAMPLES_PATH=${PROJECT_SOURCE_DIR}/samples" "ECCODES_DEFINITION_PATH=${PROJECT_SOURCE_DIR}/definitions"
ecbuild_add_test(
TARGET eccodes_c_new_sample
SOURCES new_sample.c
LIBS eccodes
ARGS "out.grib"
ENVIRONMENT "ECCODES_SAMPLES_PATH=${PROJECT_SOURCE_DIR}/samples" "ECCODES_DEFINITION_PATH=${PROJECT_SOURCE_DIR}/definitions"
)

View File

@ -0,0 +1,72 @@
/*
* Copyright 2005-2016 ECMWF.
*
* This software is licensed under the terms of the Apache Licence Version 2.0
* which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
*
* In applying this licence, ECMWF does not waive the privileges and immunities granted to it by
* virtue of its status as an intergovernmental organisation nor does it submit to any jurisdiction.
*/
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <pthread.h>
#include "eccodes.h"
#define NUM_THREADS 4
static void* process_grib(void* threadID)
{
const long tid = (long)threadID;
size_t str_len = 20;
long indicatorOfUnitOfTimeRange = 1, step = 0;
char mystring[100];
double* values = NULL;
size_t values_len = 0;
double min=0,max=0;
double pv[4]={1,2,3,4};
const size_t pvsize=4;
codes_handle* h = codes_handle_new_from_samples(0, "regular_ll_pl_grib2");
assert(h);
printf("Thread %ld running\n", tid);
CODES_CHECK(codes_set_long(h,"indicatorOfUnitOfTimeRange", indicatorOfUnitOfTimeRange),0);
CODES_CHECK(codes_set_string(h,"indicatorOfUnitOfTimeRange", "s", &str_len),0);
CODES_CHECK(codes_set_string(h,"stepUnits", "s", &str_len),0);
CODES_CHECK(codes_set_long(h, "endStep", 86400), 0);
CODES_CHECK(codes_set_long(h,"centre", 80),0);
CODES_CHECK(codes_get_long(h,"endStep", &step),0);
CODES_CHECK(codes_get_string(h, "indicatorOfUnitOfTimeRange", mystring, &str_len),0);
CODES_CHECK(codes_set_long(h,"PVPresent", 1),0);
CODES_CHECK(codes_set_double_array(h, "pv", pv, pvsize),0);
CODES_CHECK(codes_get_size(h,"values",&values_len),0);
values = (double*)malloc(values_len*sizeof(double));
CODES_CHECK(codes_get_double_array(h, "values", values, &values_len),0);
free(values);
CODES_CHECK(codes_get_double(h, "min", &min),0);
CODES_CHECK(codes_get_double(h, "max", &max),0);
codes_handle_delete(h);
pthread_exit(NULL);
}
int main(int argc, char** argv)
{
pthread_t threads[NUM_THREADS];
int error = 0;
long i = 0;
for( i=0; i<NUM_THREADS; ++i) {
printf("Creating thread %ld\n", i);
error = pthread_create(&threads[i], NULL, process_grib, (void *)i);
if (error) {
return 1;
}
}
pthread_exit(NULL);
return 0;
}

14
examples/C/grib_pthreads.sh Executable file
View File

@ -0,0 +1,14 @@
#!/bin/sh
# Copyright 2005-2016 ECMWF.
#
# This software is licensed under the terms of the Apache Licence Version 2.0
# which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
#
# In applying this licence, ECMWF does not waive the privileges and immunities granted to it by
# virtue of its status as an intergovernmental organisation nor does it submit to any jurisdiction.
. ./include.sh
for i in `seq 0 100`; do
${examples_dir}c_grib_pthreads
done