Beispiel #1
0
static void
release_symbols_lock(YogEnv* env, YogVM* vm)
{
    pthread_rwlock_unlock(&vm->sym_lock);
}
Beispiel #2
0
void *read_sock(void *p)
{
    if(p==NULL)
        return NULL;

    char szBuf[MAX_SIZE*5] = {0};
    SOCKDATA *pdata = (SOCKDATA*)p;

	int nbytes;
    if((nbytes=recv(pdata->sock_fd, szBuf, MAX_SIZE, 0))==-1 || nbytes==0){
		psyslog(LOG_NOTICE, "From client %d recv data is %s\n", pdata->sock_fd, nbytes?"not empty":"empty");
		free(pdata);
		return NULL;
	}

	uint8_t *podata = (uint8_t*)malloc(nbytes-1);
	if(reverse_crc_data(szBuf, nbytes, (void*)podata)!=0){
        psyslog(LOG_ERR, "The data from client crc error");
		free(pdata);
		free(podata);
		return NULL;
	}
	//use JSON
	cJSON *root = cJSON_Parse((char*)podata);
    if (root == NULL){
        psyslog(LOG_ERR, cJSON_GetErrorPtr());
        free(pdata);
		free(podata);
	    return NULL;
    }

    int needResponse = cJSON_GetObjectItem(root, "needResponse")->valueint;
    char *message = cJSON_GetObjectItem(root, "message")->valuestring;
    int msgLen = cJSON_GetObjectItem(root, "len")->valueint;

	bzero(szBuf, MAX_SIZE*5);
	strncpy(szBuf, message, msgLen);
	szBuf[msgLen] = '\n';

	pthread_rwlock_wrlock(&rwlock);
    write(pdata->write_fd, szBuf, msgLen+1);
	if(needResponse)
	{
		//need respons
		bzero(szBuf, MAX_SIZE*5);

		//nbytes=read(pdata->read_fd, szBuf, MAX_SIZE*5);
		sleep(0.1);
		strcpy(szBuf, "Hello WoW!");
		nbytes = strlen(szBuf);

		pdata->msg = szBuf;
		pdata->size = nbytes;
		write_sock(pdata);
	}
	pthread_rwlock_unlock(&rwlock);

	free(pdata);
	free(podata);
	cJSON_Delete(root);

	return NULL;
}
Beispiel #3
0
void fslist_unlock(fslist_p fs_p)
{
	pthread_rwlock_unlock(&fs_p->lock);
}
Beispiel #4
0
int md_cache_lru_unlock( struct md_syndicate_cache* cache ) {
   return pthread_rwlock_unlock( &cache->cache_lru_lock );
}
Beispiel #5
0
RWMutexLock::~RWMutexLock() {
    int err;
    if ((err = pthread_rwlock_unlock(&mutex.mutex)) != 0)
       Severe("Error from pthread_rwlock_unlock: %s", strerror(err));
}
Beispiel #6
0
int md_cache_promotes_unlock( struct md_syndicate_cache* cache ) {
   return pthread_rwlock_unlock( &cache->promotes_lock );
}
Beispiel #7
0
int md_cache_pending_unlock( struct md_syndicate_cache* cache ) {
   return pthread_rwlock_unlock( &cache->pending_lock );
}
Beispiel #8
0
int Mutex::unlock(std::string __file__, int __line__){
	std::string debug = this->reporte(__file__,__line__); 
	return pthread_rwlock_unlock(&mutex);
}
Beispiel #9
0
void uv_rwlock_wrunlock(uv_rwlock_t* rwlock) {
  if (pthread_rwlock_unlock(rwlock))
    abort();
}
unsigned ORBIndex::getTotalNbIndexedImages()
{
    pthread_rwlock_rdlock(&rwLock);
    return nbWords.size();
    pthread_rwlock_unlock(&rwLock);
}
Beispiel #11
0
void cRwLock::Unlock(void)
{
  pthread_rwlock_unlock(&rwlock);
}
unsigned ORBIndex::countTotalNbWord(unsigned i_imageId)
{
    pthread_rwlock_rdlock(&rwLock);
    return nbWords[i_imageId];
    pthread_rwlock_unlock(&rwLock);
}
/**
 * @brief Read the index and store it in memory.
 * @return true on success else false
 */
bool ORBIndex::readIndex(string backwardIndexPath)
{
    // Open the file.
    indexAccess = new BackwardIndexReaderFileAccess();
    if (!indexAccess->open(backwardIndexPath))
    {
        cout << "Could not open the backward index file." << endl
             << "Using an empty index." << endl;
        return false;
    }
    else
    {
        pthread_rwlock_wrlock(&rwLock);

        /* Read the table to know where are located the lines corresponding to each
         * visual word. */
        cout << "Reading the numbers of occurences." << endl;
        u_int64_t *wordOffSet = new u_int64_t[NB_VISUAL_WORDS];
        u_int64_t i_offset = NB_VISUAL_WORDS * sizeof(u_int64_t);
        for (unsigned i = 0; i < NB_VISUAL_WORDS; ++i)
        {
            indexAccess->read((char *)(nbOccurences + i), sizeof(u_int64_t));
            wordOffSet[i] = i_offset;
            i_offset += nbOccurences[i] * BACKWARD_INDEX_ENTRY_SIZE;
        }

        /* Count the number of words per image. */
        cout << "Counting the number of words per image." << endl;
        totalNbRecords = 0;
        while (!indexAccess->endOfIndex())
        {
            u_int32_t i_imageId;
            u_int16_t i_angle, x, y;
            indexAccess->read((char *)&i_imageId, sizeof(u_int32_t));
            indexAccess->read((char *)&i_angle, sizeof(u_int16_t));
            indexAccess->read((char *)&x, sizeof(u_int16_t));
            indexAccess->read((char *)&y, sizeof(u_int16_t));
            nbWords[i_imageId]++;
            totalNbRecords++;
        }

        indexAccess->reset();

        cout << "Loading the index in memory." << endl;

        for (unsigned i_wordId = 0; i_wordId < NB_VISUAL_WORDS; ++i_wordId)
        {
            indexAccess->moveAt(wordOffSet[i_wordId]);
            vector<Hit> &hits = indexHits[i_wordId];

            const unsigned i_nbOccurences = nbOccurences[i_wordId];
            hits.resize(i_nbOccurences);

            for (u_int64_t i = 0; i < i_nbOccurences; ++i)
            {
                u_int32_t i_imageId;
                u_int16_t i_angle, x, y;
                indexAccess->read((char *)&i_imageId, sizeof(u_int32_t));
                indexAccess->read((char *)&i_angle, sizeof(u_int16_t));
                indexAccess->read((char *)&x, sizeof(u_int16_t));
                indexAccess->read((char *)&y, sizeof(u_int16_t));
                hits[i].i_imageId = i_imageId;
                hits[i].i_angle = i_angle;
                hits[i].x = x;
                hits[i].y = y;
            }
        }

        delete[] wordOffSet;

        pthread_rwlock_unlock(&rwLock);

        return true;
    }
}
Beispiel #14
0
static void
release_packages_lock(YogEnv* env, YogVM* vm)
{
    pthread_rwlock_unlock(&vm->pkgs_lock);
}
Beispiel #15
0
/**
 * Releases a read/write lock.
 */
void vlc_rwlock_unlock (vlc_rwlock_t *lock)
{
    int val = pthread_rwlock_unlock (lock);
    VLC_THREAD_ASSERT ("releasing R/W lock");
}
Beispiel #16
0
static void * thread_pool_main_loop( void * arg ) {
  thread_pool_type * tp = (thread_pool_type *) arg;
  {
    const int usleep_busy = 1000;  /* The sleep time when all job slots are occupied. */
    const int usleep_init = 1000;  /* The sleep time when there are free slots available - but no jobs wanting to run. */
    int internal_offset   = 0;     /* Keep track of the (index of) the last job slot fired off - minor time saving. */
    while (true) {
      if (tp->queue_size > tp->queue_index) {
        /* 
           There are jobs in the queue which would like to run - 
           let us see if we can find a slot for them.
        */
        int counter     = 0;
        bool slot_found = false;
        do {
          int slot_index = (counter + internal_offset) % tp->max_running;
          thread_pool_job_slot_type * job_slot = &tp->job_slots[ slot_index ];
          if (!job_slot->running) {
            /* OK thread[slot_index] is ready to take this job.*/
            thread_pool_arg_type * tp_arg;
            
            /* 
               The queue might be updated by the main thread - we must
               take a copy of the node we are interested in.
            */
            pthread_rwlock_rdlock( &tp->queue_lock );
            tp_arg = util_alloc_copy( &tp->queue[ tp->queue_index ] , sizeof * tp_arg );
            pthread_rwlock_unlock( &tp->queue_lock );            

            tp_arg->slot_index = slot_index;
            job_slot->running = true;
            /* 
               Here is the actual pthread_create() call creating an
               additional running thread.
            */
            pthread_create( &job_slot->thread , NULL , thread_pool_start_job , tp_arg );
            job_slot->run_count += 1;
            tp->queue_index++;
            internal_offset += (counter + 1);
            slot_found = true;
          } else
            counter++;
        } while (!slot_found && (counter < tp->max_running));
        
        if (!slot_found)
          util_usleep( usleep_busy );  /* There are no available job slots. */
      } else
        util_usleep( usleep_init );    /* There are no jobs wanting to run. */

      /*****************************************************************/
      /*
        We exit explicitly from this loop when both conditions apply:

         1. tp->join       == true              :  The calling scope has signaled that it will not submit more jobs. 
         2. tp->queue_size == tp->queue_index   :  This function has submitted all the jobs in the queue.
      */
      if ((tp->join) &&
          (tp->queue_size == tp->queue_index))
        break;
    } /* End of while() loop */
  }

  /* 
     There are no more jobs in the queue, and the main scope has
     signaled that join should start. Observe that we join only the
     jobs corresponding to explicitly running job_slots; when a job
     slot is used multiple times the first jobs run in the job_slot
     will not be explicitly joined.
  */
  {
    int i;
    for (i=0; i < tp->max_running; i++) {
      thread_pool_job_slot_type job_slot = tp->job_slots[i];
      if (job_slot.run_count > 0)
        pthread_join( job_slot.thread , NULL );
    }
  }
  /* When we are here all the jobs have completed. */
  return NULL;
}
Beispiel #17
0
static int
do_test (void)
{
  pthread_rwlock_t r;
  int e;

  if (pthread_rwlock_init (&r, NULL) != 0)
    {
      puts ("rwlock_init failed");
      return 1;
    }
  puts ("rwlock_init succeeded");

  if (pthread_rwlock_trywrlock (&r) != 0)
    {
      puts ("rwlock_trywrlock on unlocked rwlock failed");
      return 1;
    }
  puts ("rwlock_trywrlock on unlocked rwlock succeeded");

  e = pthread_rwlock_rdlock (&r);
  if (e == 0)
    {
      puts ("rwlock_rdlock on rwlock with writer succeeded");
      return 1;
    }
  if (e != EDEADLK)
    {
      puts ("rwlock_rdlock on rwlock with writer failed != EDEADLK");
      return 1;
    }
  puts ("rwlock_rdlock on rwlock with writer failed with EDEADLK");

  e = pthread_rwlock_wrlock (&r);
  if (e == 0)
    {
      puts ("rwlock_wrlock on rwlock with writer succeeded");
      return 1;
    }
  if (e != EDEADLK)
    {
      puts ("rwlock_wrlock on rwlock with writer failed != EDEADLK");
      return 1;
    }
  puts ("rwlock_wrlock on rwlock with writer failed with EDEADLK");

  if (pthread_rwlock_unlock (&r) != 0)
    {
      puts ("rwlock_unlock failed");
      return 1;
    }
  puts ("rwlock_unlock succeeded");

  if (pthread_rwlock_destroy (&r) != 0)
    {
      puts ("rwlock_destroy failed");
      return 1;
    }
  puts ("rwlock_destroy succeeded");

  return 0;
}
Beispiel #18
0
void* threadTypeB(void* in)
{
    char ipName[20];
    #ifdef DEBUG
    int portName;
    portName=(htons(((conInfwRec*)in)->conAddr->sin_port));
    #endif
    strcpy(ipName,inet_ntoa(((conInfwRec*)in)->conAddr->sin_addr));
    #ifdef DEBUG
        printHeader(ipName,portName);
        printf("thread type B is created\n");
    #endif

    int conSocket=*(((conInfwRec*)in)->conSocket);

    msgReceiver msgRecSer(serverSocket);
    msgReceiver* msgRecCli=((conInfwRec*)in)->rec;

    MYMSG msg[LMSGL];
    char screenName[MSGL];
    bool validClient=false;

    screenName[0]=0;

    while(1)
    {
        uint32_t length;
        #ifdef DEBUG
            printHeader(ipName,portName);
            printf("thread type b: tries to get type b message\n");
        #endif
        if(msgRecCli->receiveTypeBMsg(msg,&length)==DISCONNECT)
        {
            #ifdef DEBUG
                printHeader(ipName,portName);
                printf("connection close\n");
            #endif
            if(screenName[0]==0) closeHandle(conSocket);
            else closeHandle(conSocket,scrName);

        }
        switch (msg[0])
        {
        case HELLO:
        {
            #ifdef DEBUG
                printHeader(ipName,portName);
                printf("receive a HELLO MSG, content is:\n");
                printMsg(ipName,portName,msg,length);
            #endif

            uint32_t length;
            int num;
            userInf uInf[MAXUSER];

            fetchFmsg(msg,&length,1);

            memcpy(screenName,&msg[5],length);
            screenName[length]=0;

            MYMSG sMsg[MSGL];
            length=createGetListMsg(sMsg);
            #ifdef DEBUG
                printHeader(ipName,portName);
                printf("get list message:\n");
                printMsg(ipName,portName,sMsg,length);
                printHeader(ipName,portName);
                printf("send to server\n");
            #endif

            write(serverSocket,sMsg,length);
            msgRecSer.receiveMsg(msg,&length);
            #ifdef DEBUG
                printHeader(ipName,portName);
                printf("receive a message from server, content is:\n");
                printMsg(ipName,portName,msg,length);
            #endif

            clientListParse(msg,uInf,&num);
            bool find=false;
            for(int i=0; i<num; i++)
            {
                if (strcmp(screenName,uInf[i].name)==0)
                {
                    find=true;
                    break;
                }
            }
            if (find)
            {
                #ifdef DEBUG
                    printHeader(ipName,portName);
                    printf("tries to send hello ok message\n");
                #endif
                pthread_rwlock_wrlock(&cConListLock);
                if (cConList.connectWith(screenName,conSocket,&msgRecCli)==TOOMANYCONNECTION)
                {
                    printf("Too many connections between clients\n");
                    delete msgRecCli;
                    close(conSocket);
                    err("Type B: makeConnection()\n",-9);
                }
                pthread_rwlock_unlock(&cConListLock);

                validClient=true;
                length=createHelloOkMsg(sMsg);
                #ifdef DEBUG
                    printHeader(ipName,portName);
                    printf("sending hello ok message\n");
                    printMsg(ipName,portName,sMsg,length);
                #endif
                write(conSocket,sMsg,length);
            }
            else
            {
                #ifdef DEBUG
                    printHeader(ipName,portName);
                    printf("unknown client\n");
                #endif
                length=createErrMsg(sMsg,0x04);
                #ifdef DEBUG
                    printHeader(ipName,portName);
                    printf("error msg is sent\n");
                    printMsg(ipName,portName,sMsg,length);
                #endif
                write(conSocket,sMsg,length);
            }
            break;
        }
        case MSG:
        {
            if (!validClient)
            {
                close(conSocket);
                err("Type B: validClient==false",NOSUCHUSER);
            }
            buf.addMsg(msg,screenName);
        }
        }
    }
    return NULL;
}
Beispiel #19
0
int md_cache_ongoing_writes_unlock( struct md_syndicate_cache* cache ) {
   return pthread_rwlock_unlock( &cache->ongoing_writes_lock );
}
Beispiel #20
0
void cmus_rwlock_unlock(pthread_rwlock_t *lock)
{
	int rc = pthread_rwlock_unlock(lock);
	if (unlikely(rc))
		BUG("error unlocking mutex: %s\n", strerror(rc));
}
Beispiel #21
0
int md_cache_completed_unlock( struct md_syndicate_cache* cache ) {
   return pthread_rwlock_unlock( &cache->completed_lock );
}
/**************************************************************************************
 * Function Name: appendPage
 *
 * Description:
 *		Read the requested page from the disk and append it to the tail of the PageList
 *
 * Parameters:
 *		BM_BufferPool * const bm: Buffer Pool Handler
 *		BM_PageHandle * const page: Buffer Page Handler
 *		PageNumber pageNum: the page number of the requested page
 *
 * Return:
 *		RC: return code
 *
 * Author:
 *		Xin Su <*****@*****.**>
 *
 * History:
 *		Date        Name                                Content
 *		----------  ----------------------------------  ------------------------
 *		2015-03-15  Xin Su <*****@*****.**>         Initialization
 *		2015-03-26	Xin Su <*****@*****.**>			Add thread read-write lock
 **************************************************************************************/
RC appendPage(BM_BufferPool * const bm, BM_PageHandle * const page,
		PageNumber pageNum) {
	PageList *queue = (PageList *) bm->mgmtData;
	RC rc; // init return code

	// Require lock
	pthread_rwlock_init(&rwlock, NULL);
	pthread_rwlock_wrlock(&rwlock);

	// Open File
	rc = -99;
	rc = openPageFile(bm->pageFile, F_HANDLE);

	if (rc != RC_OK) {
		return rc;
	}

	// if the size of the PageList = 0, then the PageList is empty, add this requested page as the head
	// else, the PageList is neither empty nor full, add the requested page to next of the tail of the PageList
	if (queue->size == 0) {
		queue->head->fixCount = 1;
		queue->head->numWriteIO = 1;

		// if the page does not exist, then call ensureCapacity to add the requested page to the file
		if (F_HANDLE->totalNumPages < pageNum + 1) {
			int totalPages = F_HANDLE->totalNumPages;
			rc = -99;
			rc = ensureCapacity(pageNum + 1, F_HANDLE);
			NUM_WRITE_IOS += pageNum + 1 - totalPages;

			if (rc != RC_OK) {
				// Do not change fixCount and NumWriteIO back, for this indicates write IO error and need more info to proceed

				// Close file
				rc = -99;
				rc = closePageFile(F_HANDLE);

				if (rc != RC_OK) {
					return rc;
				}

				// Release lock
				pthread_rwlock_unlock(&rwlock);
				pthread_rwlock_destroy(&rwlock);

				return rc;
			}
		}
		queue->head->numWriteIO = 0;

		// After ensureCapacity, now we can read the requested page from the file
		queue->head->numReadIO++;
		rc = -99;
		rc = readBlock(pageNum, F_HANDLE, queue->head->page->data);
		NUM_READ_IOS++;
		queue->head->numReadIO--;

		if (rc != RC_OK) {
			// Do not change fixCount and NumWriteIO back, for this indicates write IO error and need more info to proceed

			// Close file
			rc = -99;
			rc = closePageFile(F_HANDLE);

			if (rc != RC_OK) {
				return rc;
			}

			// Release lock
			pthread_rwlock_unlock(&rwlock);
			pthread_rwlock_destroy(&rwlock);

			return rc;
		}

		// Now the fixCount = 1, the numReadIO = 0, and the numWriteIO = 0
		queue->head->page->pageNum = pageNum;
		queue->head->dirtyFlag = FALSE;
		queue->head->clockFlag = FALSE;

		// Now there is only 1 page in the PageList, and all pointers , including the current pointer, are pointing to it
	} else {
		queue->tail->next->fixCount = 1;
		queue->tail->next->numWriteIO = 1;

		// if the page does not exist, then call ensureCapacity to add the requested page to the file
		if (F_HANDLE->totalNumPages < pageNum + 1) {
			int totalPages = F_HANDLE->totalNumPages;
			rc = -99;
			rc = ensureCapacity(pageNum + 1, F_HANDLE);
			NUM_WRITE_IOS += pageNum + 1 - totalPages;

			if (rc != RC_OK) {
				// Do not change fixCount and NumWriteIO back, for this indicates write IO error and need more info to proceed

				// Close file
				rc = -99;
				rc = closePageFile(F_HANDLE);

				if (rc != RC_OK) {
					return rc;
				}

				// Release lock
				pthread_rwlock_unlock(&rwlock);
				pthread_rwlock_destroy(&rwlock);

				return rc;
			}
		}
		queue->tail->next->numWriteIO = 0;

		// After ensureCapacity, now we can read the requested page from the file
		queue->tail->next->numReadIO++;
		rc = -99;
		rc = readBlock(pageNum, F_HANDLE, queue->tail->next->page->data);
		NUM_READ_IOS++;
		queue->tail->next->numReadIO--;

		if (rc != RC_OK) {
			// Do not change fixCount and NumWriteIO back, for this indicates write IO error and need more info to proceed

			// Close file
			rc = -99;
			rc = closePageFile(F_HANDLE);

			if (rc != RC_OK) {
				return rc;
			}

			// Release lock
			pthread_rwlock_unlock(&rwlock);
			pthread_rwlock_destroy(&rwlock);

			return rc;
		}

		// Now the fixCount = 1, the numReadIO = 0, and the numWriteIO = 0
		queue->tail->next->page->pageNum = pageNum;
		queue->tail->next->dirtyFlag = FALSE;
		queue->tail->next->clockFlag = FALSE;

		queue->tail = queue->tail->next;

		// Set the current pointer to the requested page, that is the tail of the PageList
		queue->current = queue->tail;
	}

	// After appending the requested page, Increment the size of the PageList
	queue->size++;

	// Load the requested page into BM_PageHandle
	page->data = queue->current->page->data;
	page->pageNum = queue->current->page->pageNum;

	// Close file
	rc = -99;
	rc = closePageFile(F_HANDLE);

	if (rc != RC_OK) {
		return rc;
	}

	// Release lock
	pthread_rwlock_unlock(&rwlock);
	pthread_rwlock_destroy(&rwlock);

	return RC_OK;
} // appendPage
int32_t ds_cont_write_unlock(hash_tbl_props_t* ds_cn) {

    return pthread_rwlock_unlock(&ds_cn->lock);
}
/**************************************************************************************
 * Function Name: forcePage
 *
 * Description:
 *		Write the requested page back to the page file on disk
 *
 * Parameters:
 *		BM_BufferPool * const bm: Buffer Pool Handler
 *		BM_PageHandle * const page: Buffer Page Handler
 *
 * Return:
 *		RC: return code
 *
 * Author:
 *		Jie Zhou <*****@*****.**>
 *
 * History:
 *		Date        Name								Content
 *		----------  ----------------------------------	----------------------------
 *		2015-03-13  Jie Zhou <*****@*****.**>		Initialization
 *		2015-03-20	Xin Su <*****@*****.**>			Modify the logic of forcing to write the requested page back
 *														Add comments
 **************************************************************************************/
RC forcePage(BM_BufferPool * const bm, BM_PageHandle * const page) {
	PageList *queue = (PageList *) bm->mgmtData;
	RC rc = -99;

	// Require lock
	pthread_rwlock_init(&rwlock, NULL);
	pthread_rwlock_wrlock(&rwlock);

	// Open file
	rc = -99;
	rc = openPageFile(bm->pageFile, F_HANDLE);

	if (rc != RC_OK) {
		return rc;
	}

	// First set numWriteIO
	queue->current->numWriteIO = 1;

	// Write the requested page back to the disk
	rc = -99;
	rc = writeBlock(page->pageNum, F_HANDLE, page->data);
	NUM_WRITE_IOS++;

	if (rc != RC_OK) {
		// Do not change fixCount and NumWriteIO back, for this indicates write IO error and need more info to proceed

		// Close file
		rc = -99;
		rc = closePageFile(F_HANDLE);

		if (rc != RC_OK) {
			return rc;
		}

		// Release unlock
		pthread_rwlock_unlock(&rwlock);
		pthread_rwlock_destroy(&rwlock);

		return rc;
	}

	// Set the page back to clean
	queue->current->dirtyFlag = FALSE;

	// The write completes without error, then set numWriteIO back
	queue->current->numWriteIO = 0;

	// Close file
	rc = -99;
	rc = closePageFile(F_HANDLE);

	if (rc != RC_OK) {
		return rc;
	}

	// Release lock
	pthread_rwlock_unlock(&rwlock);
	pthread_rwlock_destroy(&rwlock);

	return RC_OK;
} // forcePage
Beispiel #25
0
void thread_rwlock_unlock_c(rwlock_t *rwlock, int line, char *file)
{
    pthread_rwlock_unlock(&rwlock->sys_rwlock);
}
/**************************************************************************************
 * Function Name: replacePage
 *
 * Description:
 *		Replace the current page with the requested page read from the disk
 *
 * Parameters:
 *		BM_BufferPool * const bm: Buffer Pool Handler
 *		BM_PageHandle * const page: Buffer Page Handler
 *		PageNumber pageNum: the page number of the requested page
 *
 * Return:
 *		RC: return code
 *
 * Author:
 *		Xin Su <*****@*****.**>
 *
 * History:
 *		Date        Name                                Content
 *		----------  ----------------------------------  ------------------------
 *		2015-03-15  Xin Su <*****@*****.**>         Initialization
 *		2015-03-26	Xin Su <*****@*****.**>			Add thread read-write lock
 **************************************************************************************/
RC replacePage(BM_BufferPool * const bm, BM_PageHandle * const page,
		PageNumber pageNum) {
	PageList *queue = (PageList *) bm->mgmtData;
	RC rc; // init return code

	// Require lock
	pthread_rwlock_init(&rwlock, NULL);
	pthread_rwlock_wrlock(&rwlock);

	// Open file
	rc = -99;
	rc = openPageFile(bm->pageFile, F_HANDLE);

	if (rc != RC_OK) {
		return rc;
	}

	// If the removable page is dirty, then write it back to the disk before remove it.
	// Now the fixCount = 0, and the numReadIO = 0 and the numWriteIO = 0
	queue->current->fixCount = 1;
	queue->current->numWriteIO = 1;

	// if the removable page is dirty, then write it back to the file
	if (queue->current->dirtyFlag == TRUE) {
		rc = -99;
		rc = writeBlock(queue->current->page->pageNum, F_HANDLE,
				queue->current->page->data);
		NUM_WRITE_IOS++;

		if (rc != RC_OK) {
			// Do not change fixCount and NumWriteIO back, for this indicates write IO error and need more info to proceed

			// Close file
			rc = -99;
			rc = closePageFile(F_HANDLE);

			if (rc != RC_OK) {
				return rc;
			}

			// Release unlock
			pthread_rwlock_unlock(&rwlock);
			pthread_rwlock_destroy(&rwlock);

			return rc;
		}

		// After writeBlock, set the PageFrame back to clean
		queue->current->dirtyFlag = FALSE;
	}

	// if the page does not exist, then call ensureCapacity to add the requested page to the file
	if (F_HANDLE->totalNumPages < pageNum + 1) {
		int totalPages = F_HANDLE->totalNumPages;
		rc = -99;
		rc = ensureCapacity(pageNum + 1, F_HANDLE);
		NUM_WRITE_IOS += pageNum + 1 - totalPages;

		if (rc != RC_OK) {
			// Do not change fixCount and NumWriteIO back, for this indicates write IO error and need more info to proceed

			// Close file
			rc = -99;
			rc = closePageFile(F_HANDLE);

			if (rc != RC_OK) {
				return rc;
			}

			// Release unlock
			pthread_rwlock_unlock(&rwlock);
			pthread_rwlock_destroy(&rwlock);

			return rc;
		}
	}
	queue->current->numWriteIO = 0;

	// After ensureCapacity, now we can read the requested page from the file
	queue->current->numReadIO++;
	rc = -99;
	rc = readBlock(pageNum, F_HANDLE, queue->current->page->data);
	NUM_READ_IOS++;
	queue->current->numReadIO--;

	if (rc != RC_OK) {
		// Do not change fixCount and NumWriteIO back, for this indicates write IO error and need more info to proceed

		// Close file
		rc = -99;
		rc = closePageFile(F_HANDLE);

		if (rc != RC_OK) {
			return rc;
		}

		// Release lock
		pthread_rwlock_unlock(&rwlock);
		pthread_rwlock_destroy(&rwlock);

		return rc;
	}

	// Load the requested page to the current PageFrame in the BM_BufferPool
	// Now the fixCount = 1, the numReadIO = 0, and the numWriteIO = 0
	queue->current->page->pageNum = pageNum;
	queue->current->clockFlag = FALSE;

	// Load the requested into BM_PageHandle
	page->data = queue->current->page->data;
	page->pageNum = queue->current->page->pageNum;

	// Close file
	rc = -99;
	rc = closePageFile(F_HANDLE);

	if (rc != RC_OK) {
		return rc;
	}

	// Release lock
	pthread_rwlock_unlock(&rwlock);
	pthread_rwlock_destroy(&rwlock);

	return RC_OK;
} // replacePage
Beispiel #27
0
static int
do_test (void)
{
  pthread_rwlock_t r;

  if (pthread_rwlock_init (&r, NULL) != 0)
    {
      puts ("rwlock_init failed");
      return 1;
    }
  puts ("rwlock_init succeeded");

  if (pthread_rwlock_rdlock (&r) != 0)
    {
      puts ("1st rwlock_rdlock failed");
      return 1;
    }
  puts ("1st rwlock_rdlock succeeded");

  if (pthread_rwlock_rdlock (&r) != 0)
    {
      puts ("2nd rwlock_rdlock failed");
      return 1;
    }
  puts ("2nd rwlock_rdlock succeeded");

  if (pthread_rwlock_unlock (&r) != 0)
    {
      puts ("1st rwlock_unlock failed");
      return 1;
    }
  puts ("1st rwlock_unlock succeeded");

  if (pthread_rwlock_unlock (&r) != 0)
    {
      puts ("2nd rwlock_unlock failed");
      return 1;
    }
  puts ("2nd rwlock_unlock succeeded");

  if (pthread_rwlock_wrlock (&r) != 0)
    {
      puts ("1st rwlock_wrlock failed");
      return 1;
    }
  puts ("1st rwlock_wrlock succeeded");

  if (pthread_rwlock_unlock (&r) != 0)
    {
      puts ("3rd rwlock_unlock failed");
      return 1;
    }
  puts ("3rd rwlock_unlock succeeded");

  if (pthread_rwlock_wrlock (&r) != 0)
    {
      puts ("2nd rwlock_wrlock failed");
      return 1;
    }
  puts ("2nd rwlock_wrlock succeeded");

  if (pthread_rwlock_unlock (&r) != 0)
    {
      puts ("4th rwlock_unlock failed");
      return 1;
    }
  puts ("4th rwlock_unlock succeeded");

  if (pthread_rwlock_rdlock (&r) != 0)
    {
      puts ("3rd rwlock_rdlock failed");
      return 1;
    }
  puts ("3rd rwlock_rdlock succeeded");

  if (pthread_rwlock_unlock (&r) != 0)
    {
      puts ("5th rwlock_unlock failed");
      return 1;
    }
  puts ("5th rwlock_unlock succeeded");

  if (pthread_rwlock_destroy (&r) != 0)
    {
      puts ("rwlock_destroy failed");
      return 1;
    }
  puts ("rwlock_destroy succeeded");

  return 0;
}
Beispiel #28
0
static PVOID MV_SHM_GetVirtAddr_Base(shm_dev_t *shm_dev, size_t Offset)
{
	shm_address_t *address_node;
	shm_driver_operation_t op;
	shm_free_t shm_free;
    ldns_rbnode_t *node;

	if ((SHM_DEVICE_LOAD_COUNT <= 0) ||
		((Offset >= shm_dev->base.m_size) &&((shm_dev->mem_type == SHM_CACHE)
			|| (shm_dev->mem_type == SHM_NONCACHE)))) {
		MV_SHM_Print("user space MV_SHM_GetVirtAddr_Base shm device not"
			" open or parameter fail. open[%d] offset[%08x] >= "
			"shm_size[%08x] mem_type[%d]\n", SHM_DEVICE_LOAD_COUNT,
			Offset, shm_dev->base.m_size, shm_dev->mem_type);
		return NULL;
	}

	pthread_rwlock_wrlock(&shm_dev->addr.m_rb_rwlock);
	address_node = MV_SHM_lookup_phyaddress_node(
			&(shm_dev->addr.m_phyaddr_root),
			(shm_dev->base.m_base_physaddr + Offset));

	if (address_node == NULL) {
		address_node = malloc_shm_node();
		if(address_node == NULL) {
			MV_SHM_Print("user space MV_SHM_GetVirtAddr_Base"
				" malloc fail\n");
			pthread_rwlock_unlock(&shm_dev->addr.m_rb_rwlock);
			return NULL;
		}

		op.m_param1 = shm_dev->base.m_base_physaddr + Offset;
		if (MV_SHM_mmap_preparation(shm_dev->base.m_fd, &op) != 0) {
			MV_SHM_Print("user space MV_SHM_GetVirtAddr_Base "
				"MV_SHM_mmap_preparation fail\n");
			free(address_node);
			pthread_rwlock_unlock(&shm_dev->addr.m_rb_rwlock);
			return NULL;
		}

		address_node->m_phyaddress = op.m_param1;
		address_node->m_size = op.m_param2;
		address_node->m_virtaddress = MV_SHM_Mmap_Base(shm_dev,
			address_node->m_phyaddress, address_node->m_size);

		if (address_node->m_virtaddress == (size_t)MAP_FAILED) {
			MV_SHM_Print("user space MV_SHM_GetVirtAddr_Base "
				"MV_SHM_Mmap_Base fail\n");
			free(address_node);
			pthread_rwlock_unlock(&shm_dev->addr.m_rb_rwlock);
			MV_SHM_DumpProcessMemoryMap();
			return NULL;
		}

		if (MV_SHM_insert_phyaddress_node(
			&(shm_dev->addr.m_phyaddr_root), address_node) == NULL) {
            MV_SHM_Print("SHM:GETV:PHY:0x%x,%d\n", Offset, __LINE__);
			MV_SHM_Dump_Node(&(shm_dev->addr.m_phyaddr_root));
            node = MV_SHM_delete_phyaddress_node(&(shm_dev->addr.m_phyaddr_root), \
                address_node);
            MV_SHM_delete_virtaddress_node(&(shm_dev->addr.m_virtaddr_root), \
                node->key);
            MV_SHM_insert_phyaddress_node(
			&(shm_dev->addr.m_phyaddr_root), address_node);
        }
	    if (MV_SHM_insert_virtaddress_node(
			&(shm_dev->addr.m_virtaddr_root), address_node) == NULL) {
			MV_SHM_Print("SHM:GETV:VIRT:0x%x,%d\n", Offset, __LINE__);
			MV_SHM_Dump_Node(&(shm_dev->addr.m_virtaddr_root));
            node = MV_SHM_delete_virtaddress_node(&(shm_dev->addr.m_virtaddr_root), \
                address_node);
            MV_SHM_delete_phyaddress_node(&(shm_dev->addr.m_phyaddr_root), \
                node->key);
            MV_SHM_insert_virtaddress_node(&(shm_dev->addr.m_virtaddr_root),
                address_node);
        }
		pthread_rwlock_unlock(&shm_dev->addr.m_rb_rwlock);
		return (PVOID)(address_node->m_virtaddress+
				((Offset + shm_dev->base.m_base_physaddr)
				- address_node->m_phyaddress));;
	} else {
		op.m_param1 = shm_dev->base.m_base_physaddr + Offset;
		if (MV_SHM_mmap_preparation(shm_dev->base.m_fd, &op) != 0) {
			MV_SHM_Print("user space MV_SHM_GetVirtAddr_Base 2 "
				"MV_SHM_mmap_preparation fail\n");
			pthread_rwlock_unlock(&shm_dev->addr.m_rb_rwlock);
			return NULL;
		}

		if ((address_node->m_size == op.m_param2) &&
			(address_node->m_phyaddress == op.m_param1)) {
			pthread_rwlock_unlock(&shm_dev->addr.m_rb_rwlock);
			return (PVOID)(address_node->m_virtaddress+
				((Offset + shm_dev->base.m_base_physaddr)
				- address_node->m_phyaddress));
		} else {
			shm_free.address = op.m_param1;
			shm_free.size = op.m_param2;
			shm_free.shm_root = &shm_dev->addr;
			shm_free.mem_type = shm_dev->mem_type;
			MV_SHM_free_all_node(&shm_free);

			address_node = shm_free.node;
			if (shm_free.flag == 0) {
				address_node->m_phyaddress = op.m_param1;
				address_node->m_size = op.m_param2;
				address_node->m_virtaddress = MV_SHM_Mmap_Base(shm_dev,
					address_node->m_phyaddress, address_node->m_size);

				if (address_node->m_virtaddress == (size_t)MAP_FAILED) {
					MV_SHM_Print("user space MV_SHM_GetVirtAddr_Base 4 "
						"MV_SHM_Mmap_Base fail\n");
					free(address_node);
					pthread_rwlock_unlock(&shm_dev->addr.m_rb_rwlock);
					MV_SHM_DumpProcessMemoryMap();
					return NULL;
				}

                if (MV_SHM_insert_phyaddress_node(
                    &(shm_dev->addr.m_phyaddr_root), address_node) == NULL) {
                    MV_SHM_Print("SHM:GETV:PHY:0x%x,%d\n", Offset, __LINE__);
                    MV_SHM_Dump_Node(&(shm_dev->addr.m_phyaddr_root));
                    node = MV_SHM_delete_phyaddress_node(&(shm_dev->addr.m_phyaddr_root), \
                        address_node);
                    MV_SHM_delete_virtaddress_node(&(shm_dev->addr.m_virtaddr_root), \
                        node->key);
                    MV_SHM_insert_phyaddress_node(
                    &(shm_dev->addr.m_phyaddr_root), address_node);
                }
                if (MV_SHM_insert_virtaddress_node(
                    &(shm_dev->addr.m_virtaddr_root), address_node) == NULL) {
                    MV_SHM_Print("SHM:GETV:VIRT:0x%x,%d\n", Offset, __LINE__);
                    MV_SHM_Dump_Node(&(shm_dev->addr.m_virtaddr_root));
                    node = MV_SHM_delete_virtaddress_node(&(shm_dev->addr.m_virtaddr_root), \
                        address_node);
                    MV_SHM_delete_phyaddress_node(&(shm_dev->addr.m_phyaddr_root), \
                        node->key);
                    MV_SHM_insert_virtaddress_node(&(shm_dev->addr.m_virtaddr_root),
                        address_node);
                }
			}
			pthread_rwlock_unlock(&shm_dev->addr.m_rb_rwlock);
			return (PVOID)(address_node->m_virtaddress+
				((Offset + shm_dev->base.m_base_physaddr)
					- address_node->m_phyaddress));;
		}
	}
}
Beispiel #29
0
void BufferFrame::unlock() {
	pthread_rwlock_unlock(&header.rwlock);
}
Beispiel #30
0
void UpdateLock::unlock()
{
   pthread_rwlock_unlock(& m_update_lock) ;
}