/* * Copyright 2008, Yahoo! Inc. * * Licensed 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 * * http://www.apache.org/licenses/LICENSE-2.0 * * 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. */ #include #include "zk_log.h" #include #include #include #include static zhandle_t *zh; static int shutdownThisThing=0; // ***************************************************************************** // static pthread_cond_t cond=PTHREAD_COND_INITIALIZER; static pthread_mutex_t lock=PTHREAD_MUTEX_INITIALIZER; static pthread_cond_t counterCond=PTHREAD_COND_INITIALIZER; static pthread_mutex_t counterLock=PTHREAD_MUTEX_INITIALIZER; static int counter; void ensureConnected(){ pthread_mutex_lock(&lock); while (zoo_state(zh)!=CONNECTED_STATE) { pthread_cond_wait(&cond,&lock); } pthread_mutex_unlock(&lock); } void incCounter(int delta){ pthread_mutex_lock(&counterLock); counter+=delta; pthread_cond_broadcast(&counterCond); pthread_mutex_unlock(&counterLock); } void setCounter(int cnt){ pthread_mutex_lock(&counterLock); counter=cnt; pthread_cond_broadcast(&counterCond); pthread_mutex_unlock(&counterLock); } void waitCounter(){ pthread_mutex_lock(&counterLock); while (counter>0) { pthread_cond_wait(&counterCond,&counterLock); } pthread_mutex_unlock(&counterLock); } void listener(zhandle_t *zzh, int type, int state, const char *path) { if(type == SESSION_EVENT){ if(state == CONNECTED_STATE){ pthread_mutex_lock(&lock); pthread_cond_broadcast(&cond); pthread_mutex_unlock(&lock); } setCounter(0); } } void create_completion(int rc, const char *name, const void *data) { incCounter(-1); if(rc!=ZOK){ LOG_ERROR(("Failed to create a node rc=%d",rc)); } } int doCreateNodes(const char* root, int count){ char nodeName[1024]; int i; for(i=0; idata) { int32_t i; for(i=0;icount; i++) { free(v->data[i]); } free(v->data); v->data = 0; } return 0; } static int deletedCounter; int recursiveDelete(const char* root){ struct String_vector children; int i; int rc=zoo_get_children(zh,root,0,&children); if(rc!=ZNONODE){ if(rc!=ZOK){ LOG_ERROR(("Failed to get children of %s, rc=%d",root,rc)); return rc; } for(i=0;i