/** * Request Reader Start */ static int ngislRequestReaderStart( ngisiContext_t *context, int *error) { static const char fName[] = "ngislRequestReaderStart"; ngisiRequestReader_t *requestReader; int result; /* Check the arguments */ assert(context != NULL); requestReader = &context->ngisc_requestReader; requestReader->ngisr_continue = 1; requestReader->ngisr_working = 1; /* Create the Request Reader thread */ result = globus_thread_create( &requestReader->ngisr_thread, NULL, ngislRequestReaderThread, context); if (result != 0) { ngisiLogPrintf(context, NGISI_LOG_LEVEL_ERROR, fName, "Create the Request Reader thread failed.\n"); return 0; } /* Success */ return 1; }
/** * RefreshCredential: Start thread. */ static int ngislRefreshCredentialThreadStart( ngisiContext_t *context, ngisiRefreshCredential_t *ref, int *error) { static const char fName[] = "ngislRefreshCredentialThreadStart"; int result; /* Check the arguments */ assert(context != NULL); assert(ref != NULL); if (ref->ngisrc_working != 0) { ngisiLogPrintf(context, NGISI_LOG_LEVEL_ERROR, fName, "Refresh Credential thread is already working.\n"); return 0; } /* Check if the flag is valid */ assert(ref->ngisrc_continue == 0); ref->ngisrc_continue = 1; /* TRUE */ /* log */ ngisiLogPrintf(context, NGISI_LOG_LEVEL_DEBUG, fName, "Create the Refresh Credential thread.\n"); /* Create the Refresh Credential thread */ result = globus_thread_create( &ref->ngisrc_thread, NULL, ngislRefreshCredentialThread, context); if (result != 0) { ngisiLogPrintf(context, NGISI_LOG_LEVEL_ERROR, fName, "Can't create thread for Refresh Credential.\n"); return 0; } /* Success */ return 1; }
int main( int argc, char * argv[]) { int i; int nitems; long thread_id; globus_thread_t thread; globus_thread_set_model("pthread"); globus_module_activate(GLOBUS_COMMON_MODULE); if (argc != 4) { globus_stdio_lock(); { printf("\nusage: globus_thread_test " "nproducers nconsumers nitems\n\n"); } globus_stdio_unlock(); exit(1); } nproducers = atoi(argv[1]); nconsumers = atoi(argv[2]); nitems = atoi(argv[3]); /* * Initialize queue and queue concurrency control structures */ globus_fifo_init(&queue); globus_mutex_init(&queue_mutex, (globus_mutexattr_t *) GLOBUS_NULL); globus_cond_init(&queue_cond, (globus_condattr_t *) GLOBUS_NULL); /* * Initialize shared (common) concurrency control structures */ globus_mutex_init(&common_mutex, (globus_mutexattr_t *) GLOBUS_NULL); globus_cond_init(&common_cond, (globus_condattr_t *) GLOBUS_NULL); /* * Assign a thread id to the main thread so that it's output is uniquely * tagged. Note: we do not use the return value of globus_thread_self() * since it could be a pointer or a structure, the latter which is * extremely hard to print without knowing the implementation details. */ thread_id_assign(); thread_id = thread_id_get(); /* * Start producer and consumer threads */ globus_stdio_lock(); { printf("%04ld: main() - starting %d producer and %d consumer threads\n", thread_id, nproducers, nconsumers); } globus_stdio_unlock(); for (i = 0 ; i < nproducers ; i ++) { int rc; int nitems_per_thread; nitems_per_thread = nitems / nproducers + ((i < nitems % nproducers) ? 1 : 0); rc = globus_thread_create( &thread, NULL, producer, (void *) nitems_per_thread); if (rc != 0) { globus_stdio_lock(); { printf("%04ld: main() - ERROR: " "unable to create producer thread %d\n", thread_id, i); exit(1); } globus_stdio_unlock(); } } for (i = 0 ; i < nconsumers ; i ++) { int rc; int nitems_per_thread; nitems_per_thread = nitems / nconsumers + ((i < nitems % nconsumers) ? 1 : 0); rc = globus_thread_create( &thread, NULL, consumer, (void *) nitems_per_thread); if (rc != 0) { globus_stdio_lock(); { printf("%04ld: main() - ERROR: " "unable to create consumer thread %d\n", thread_id, i); exit(1); } globus_stdio_unlock(); } } /* * Wait for all threads to be started */ wait_for_all(); globus_stdio_lock(); { printf("%04ld: main() - all threads started\n", thread_id); } globus_stdio_unlock(); /* * Wait for all threads to complete their work */ wait_for_all(); globus_stdio_lock(); { printf("%04ld: main() - all threads have completed their work\n", thread_id); } globus_stdio_unlock(); /* * Wait for all thread id data to be destroyed */ while (thread_ids_destruct_cnt < nproducers + nconsumers) { globus_thread_yield(); } globus_stdio_lock(); { printf("%04ld: main() - all threads terminated\n", thread_id); } globus_stdio_unlock(); globus_cond_destroy(&common_cond); globus_mutex_destroy(&common_mutex); globus_cond_destroy(&queue_cond); globus_mutex_destroy(&queue_mutex); globus_fifo_destroy(&queue); globus_module_deactivate(GLOBUS_COMMON_MODULE); exit(0); }
int main() { gss_cred_id_t credential; int listen_fd; int accept_fd; struct sockaddr_in address = {0}; globus_sockaddr_t connect_address; globus_socklen_t connect_address_len = sizeof(connect_address); struct thread_arg * arg = NULL; globus_thread_t thread_handle; int i; int ret; int error; LTDL_SET_PRELOADED_SYMBOLS(); globus_thread_set_model(THREAD_MODEL); globus_module_activate(GLOBUS_COMMON_MODULE); globus_module_activate(GLOBUS_GSI_GSSAPI_MODULE); printf("1..%d\n", NUM_CLIENTS); /* initialize global mutex */ globus_mutex_init(&mutex, NULL); /* and the condition variable */ globus_cond_init(&done, NULL); /* setup listener */ address.sin_family = AF_INET; address.sin_port = 0; address.sin_addr.s_addr = htonl(INADDR_LOOPBACK); listen_fd = socket(AF_INET, SOCK_STREAM, 0); if (listen_fd == 0) { perror("socket"); exit(EXIT_FAILURE); } ret = bind(listen_fd, (struct sockaddr *) &address, sizeof(struct sockaddr_in)); if (ret != 0) { perror("bind"); exit(EXIT_FAILURE); } ret = getsockname(listen_fd, (struct sockaddr *) &connect_address, &connect_address_len); if (ret != 0) { perror("getsockname"); exit(EXIT_FAILURE); } ret = listen(listen_fd, -1); if (ret != 0) { perror("listen"); exit(EXIT_FAILURE); } /* acquire credentials */ credential = globus_gsi_gssapi_test_acquire_credential(); if(credential == GSS_C_NO_CREDENTIAL) { fprintf(stderr,"Unable to aquire credential\n"); exit(-1); } /* start the clients here */ for(i=0;i<NUM_CLIENTS;i++) { arg = malloc(sizeof(struct thread_arg)); arg->address = &connect_address; arg->len = connect_address_len; arg->credential = credential; globus_mutex_lock(&mutex); { client_thread_count++; } globus_mutex_unlock(&mutex); globus_thread_create(&thread_handle,NULL,client_func,(void *) arg); } /* accept connections */ globus_mutex_lock(&mutex); while (client_thread_count > 0) { while (pending_connects > 0) { accept_fd = accept(listen_fd,NULL,0); if(accept_fd < 0) { perror("accept"); abort(); } arg = malloc(sizeof(struct thread_arg)); arg->fd = accept_fd; arg->credential = credential; server_thread_count++; pending_connects--; globus_thread_create(&thread_handle,NULL,server_func,(void *) arg); } globus_cond_wait(&done, &mutex); } /* wait for last thread to terminate */ while (server_thread_count > 0) { globus_cond_wait(&done, &mutex); } globus_mutex_unlock(&mutex); /* destroy global mutex */ globus_mutex_destroy(&mutex); /* and the condition variable */ globus_cond_destroy(&done); /* close the listener */ close(listen_fd); /* release credentials */ globus_gsi_gssapi_test_release_credential(&credential); globus_module_deactivate_all(); exit(client_failed); }
/** * ObserveThread: Start observing */ int ngcliObserveThreadStart( ngclContext_t *context, int *error) { static const char fName[] = "ngcliObserveThreadStart"; ngcliObserveThread_t *observe; int result, enabled; /* Check the arguments */ assert(context != NULL); observe = &context->ngc_observe; enabled = 0; if (observe->ngot_item_head != NULL) { enabled = 1; } if (enabled == 0) { ngclLogPrintfContext(context, NG_LOG_CATEGORY_NINFG_PURE, NG_LOG_LEVEL_INFORMATION, NULL, "%s: Observe Thread is unnecessary.\n", fName); /* Do nothing */ observe->ngot_continue = 0; /* FALSE */ observe->ngot_stopped = 0; /* FALSE */ return 1; } #ifdef NG_PTHREAD observe->ngot_continue = 1; /* TRUE */ observe->ngot_stopped = 0; /* FALSE */ /* log */ ngclLogPrintfContext(context, NG_LOG_CATEGORY_NINFG_PURE, NG_LOG_LEVEL_DEBUG, NULL, "%s: Start the Observe Thread.\n", fName); /* Create the Observe Thread */ result = globus_thread_create( &observe->ngot_thread, NULL, ngcllObserveThread, context); if (result != 0) { NGI_SET_ERROR(error, NG_ERROR_GLOBUS); ngclLogPrintfContext(context, NG_LOG_CATEGORY_NINFG_PURE, NG_LOG_LEVEL_ERROR, NULL, "%s: Can't create the thread for observe.\n", fName); return 0; } #else /* NG_PTHREAD */ /* Do nothing */ result = 1; assert(result != 0); observe->ngot_continue = 0; /* FALSE */ observe->ngot_stopped = 0; /* FALSE */ ngclLogPrintfContext(context, NG_LOG_CATEGORY_NINFG_PURE, NG_LOG_LEVEL_ERROR, NULL, "%s: observing is not supported for this GlobusToolkit flavor.\n", fName); ngclLogPrintfContext(context, NG_LOG_CATEGORY_NINFG_PURE, NG_LOG_LEVEL_ERROR, NULL, "%s: observing is supported only for pthread version.\n", fName); #endif /* NG_PTHREAD */ /* Success */ return 1; }
/************************************************************************* * start * ----- * This function is called when a new session is initialized, ie a user * connects to the server. This hook gives the dsi an opportunity to * set internal state that will be threaded through to all other * function calls associated with this session. And an opportunity to * reject the user. * * finished_info.info.session.session_arg should be set to an DSI * defined data structure. This pointer will be passed as the void * * user_arg parameter to all other interface functions. * * NOTE: at nice wrapper function should exist that hides the details * of the finished_info structure, but it currently does not. * The DSI developer should jsut follow this template for now ************************************************************************/ static void globus_l_dsi_rest_start( globus_gfs_operation_t op, globus_gfs_session_info_t * session_info) { globus_l_dsi_rest_handle_t *dsi_rest_handle; globus_result_t result = GLOBUS_SUCCESS; dsi_rest_handle = malloc(sizeof(globus_l_dsi_rest_handle_t)); if (dsi_rest_handle == NULL) { result = GlobusGFSErrorMemory("dsi_rest_handle"); goto handle_malloc_fail; } result = globus_xio_driver_load("tcp", &dsi_rest_handle->tcp_driver); if (result != GLOBUS_SUCCESS) { goto tcp_load_fail; } result = globus_xio_driver_load("http", &dsi_rest_handle->http_driver); if (result != GLOBUS_SUCCESS) { goto http_load_fail; } result = globus_xio_stack_init(&dsi_rest_handle->server_stack, NULL); if (result != GLOBUS_SUCCESS) { goto stack_init_fail; } globus_mutex_init(&dsi_rest_handle->mutex, NULL); globus_cond_init(&dsi_rest_handle->cond, NULL); dsi_rest_handle->terminate = false; dsi_rest_handle->terminate_complete = false; result = globus_xio_stack_push_driver( dsi_rest_handle->server_stack, dsi_rest_handle->tcp_driver); if (result != GLOBUS_SUCCESS) { goto stack_push_fail; } result = globus_xio_stack_push_driver( dsi_rest_handle->server_stack, dsi_rest_handle->http_driver); if (result != GLOBUS_SUCCESS) { goto stack_push_fail; } globus_xio_attr_t attr; result = globus_xio_attr_init(&attr); globus_reltime_t delay; GlobusTimeReltimeSet(delay, 1,0); result = globus_xio_attr_cntl( attr, NULL, GLOBUS_XIO_ATTR_SET_TIMEOUT_ACCEPT, NULL, &delay, NULL); result = globus_xio_server_create( &dsi_rest_handle->xio_server, attr, dsi_rest_handle->server_stack); if (result != GLOBUS_SUCCESS) { goto server_create_fail; } result = globus_xio_server_get_contact_string( dsi_rest_handle->xio_server, &dsi_rest_handle->contact_string); if (result != GLOBUS_SUCCESS) { goto get_contact_fail; } globus_gridftp_server_get_config_string( op, &dsi_rest_handle->root); if (dsi_rest_handle->root == NULL) { result = GlobusGFSErrorMemory("root"); goto get_root_fail; } globus_thread_create( &dsi_rest_handle->server_thread, NULL, globus_l_dsi_rest_thread, dsi_rest_handle); if (result != GLOBUS_SUCCESS) { get_root_fail: free(dsi_rest_handle->contact_string); get_contact_fail: globus_xio_server_close(dsi_rest_handle->xio_server); server_create_fail: stack_push_fail: globus_xio_stack_destroy(dsi_rest_handle->server_stack); stack_init_fail: globus_xio_driver_unload(dsi_rest_handle->http_driver); http_load_fail: globus_xio_driver_unload(dsi_rest_handle->tcp_driver); tcp_load_fail: handle_malloc_fail: free(dsi_rest_handle); dsi_rest_handle = NULL; } globus_gridftp_server_finished_session_start( op, result, dsi_rest_handle, NULL, NULL); }