Exemplo n.º 1
0
BOOL ConsumeElement(int nThreadNum, int nRequestNum, HWND hWndLB) {

   // Get access to the queue to consume a new element
   AcquireSRWLockShared(&g_srwLock); 

   // Fall asleep until there is something to read.
   // Check if, while it was asleep, 
   // it was not decided that the thread should stop
   while (g_q.IsEmpty(nThreadNum) && !g_fShutdown) {
      // There was not a readable element
      AddText(hWndLB, TEXT("[%d] Nothing to process"), nThreadNum);
         
      // The queue is empty
      // --> Wait until a writer adds a new element to read
      //     and come back with the lock acquired in shared mode
      SleepConditionVariableSRW(&g_cvReadyToConsume, &g_srwLock, 
         INFINITE, CONDITION_VARIABLE_LOCKMODE_SHARED);
   }

   // When thread is exiting, the lock should be released for writer
   // and readers should be signaled through the condition variable
   if (g_fShutdown) {
      // Show that the current thread is exiting
      AddText(hWndLB, TEXT("[%d] bye bye"), nThreadNum);

      // Another writer thread might still be blocked on the lock
      // --> release it before exiting
      ReleaseSRWLockShared(&g_srwLock);

      // Notify other readers that it is time to exit
      // --> release readers
      WakeConditionVariable(&g_cvReadyToConsume);

      return(FALSE);
   }

   // Get the first new element
   CQueue::ELEMENT e;
   // Note: No need to test the return value since IsEmpty
   //       returned FALSE
   g_q.GetNewElement(nThreadNum, e);
      
   // No need to keep the lock any longer
   ReleaseSRWLockShared(&g_srwLock);

   // Show result of consuming the element
   AddText(hWndLB, TEXT("[%d] Processing %d:%d"), 
      nThreadNum, e.m_nThreadNum, e.m_nRequestNum);

   // A free slot is now available for writer threads to produce
   // --> wake up a writer thread
   WakeConditionVariable(&g_cvReadyToProduce);

   return(TRUE);
}
Exemplo n.º 2
0
static size_t winfs_read(struct file *f, void *buf, size_t count)
{
	AcquireSRWLockShared(&f->rw_lock);
	struct winfs_file *winfile = (struct winfs_file *) f;
	WaitForSingleObject(winfile->fp_mutex, INFINITE);
	size_t num_read = 0;
	while (count > 0)
	{
		DWORD count_dword = (DWORD)min(count, (size_t)UINT_MAX);
		DWORD num_read_dword;
		if (!ReadFile(winfile->handle, buf, count_dword, &num_read_dword, NULL))
		{
			if (GetLastError() == ERROR_HANDLE_EOF)
				break;
			log_warning("ReadFile() failed, error code: %d", GetLastError());
			num_read = -L_EIO;
			break;
		}
		if (num_read_dword == 0)
			break;
		num_read += num_read_dword;
		count -= num_read_dword;
	}
	ReleaseMutex(winfile->fp_mutex);
	ReleaseSRWLockShared(&f->rw_lock);
	return num_read;
}
Exemplo n.º 3
0
static int winfs_statfs(struct file *f, struct statfs64 *buf)
{
	AcquireSRWLockShared(&f->rw_lock);
	struct winfs_file *winfile = (struct winfs_file *) f;
	FILE_FS_FULL_SIZE_INFORMATION info;
	IO_STATUS_BLOCK status_block;
	NTSTATUS status = NtQueryVolumeInformationFile(winfile->handle, &status_block, &info, sizeof(info), FileFsFullSizeInformation);
	int r = 0;
	if (!NT_SUCCESS(status))
	{
		log_warning("NtQueryVolumeInformationFile() failed, status: %x", status);
		r = -L_EIO;
		goto out;
	}
	buf->f_type = 0x5346544e; /* NTFS_SB_MAGIC */
	buf->f_bsize = info.SectorsPerAllocationUnit * info.BytesPerSector;
	buf->f_blocks = info.TotalAllocationUnits.QuadPart;
	buf->f_bfree = info.ActualAvailableAllocationUnits.QuadPart;
	buf->f_bavail = info.CallerAvailableAllocationUnits.QuadPart;
	buf->f_files = 0;
	buf->f_ffree = 0;
	buf->f_fsid.val[0] = 0;
	buf->f_fsid.val[1] = 0;
	buf->f_namelen = PATH_MAX;
	buf->f_frsize = 0;
	buf->f_flags = 0;
	buf->f_spare[0] = 0;
	buf->f_spare[1] = 0;
	buf->f_spare[2] = 0;
	buf->f_spare[3] = 0;
out:
	ReleaseSRWLockShared(&f->rw_lock);
	return r;
}
Exemplo n.º 4
0
static int winfs_link(struct mount_point *mp, struct file *f, const char *newpath)
{
	AcquireSRWLockShared(&f->rw_lock);
	struct winfs_file *winfile = (struct winfs_file *) f;
	NTSTATUS status;
	int r = 0;
	char buf[sizeof(FILE_LINK_INFORMATION) + PATH_MAX * 2];
	FILE_LINK_INFORMATION *info = (FILE_LINK_INFORMATION *)buf;
	info->ReplaceIfExists = FALSE;
	info->RootDirectory = NULL;
	info->FileNameLength = 2 * filename_to_nt_pathname(mp, newpath, info->FileName, PATH_MAX);
	if (info->FileNameLength == 0)
	{
		r = -L_ENOENT;
		goto out;
	}
	IO_STATUS_BLOCK status_block;
	status = NtSetInformationFile(winfile->handle, &status_block, info, info->FileNameLength + sizeof(FILE_LINK_INFORMATION), FileLinkInformation);
	if (!NT_SUCCESS(status))
	{
		log_warning("NtSetInformationFile() failed, status: %x.", status);
		r = -L_ENOENT;
		goto out;
	}
out:
	ReleaseSRWLockShared(&f->rw_lock);
	return r;
}
Exemplo n.º 5
0
	void ui::sendmouse(char param, vector2d pos)
	{
		mouse.update(pos);

		uielement* front = 0;
		if (actionsenabled)
		{
			if (TryAcquireSRWLockShared(&uilock))
			{
				for (map<char, uielement*>::iterator elit = elements.begin(); elit != elements.end(); elit++)
				{
					if (util::colliding(pos, elit->second->bounds()) && elit->second->isactive())
					{
						if (front != 0)
							front->sendmouse(pos, -1);
						front = elit->second;
					}
				}
				ReleaseSRWLockShared(&uilock);
			}
		}

		if (front != 0)
		{
			mouse.setstate(front->sendmouse(pos, param));
		}
		else
		{
			mouse.setstate(param);

			if (mouse.getstate() == 1)
				mouse.setstate(0);
		}
	}
Exemplo n.º 6
0
 void SimpleRWLock::unlock_shared() { 
     int& state = s.getRef();
     dassert( state == 1 );
     state--;
     shares--;
     ReleaseSRWLockShared(&_lock); 
 }
Exemplo n.º 7
0
int  rwmutex_rdunlock(XQ_rwmutex_t *m)
{
#if _WIN32_WINNT >= 0x0700
    ReleaseSRWLockShared(m);
#endif
    return 0;
}
Exemplo n.º 8
0
void nfs41_session_free_slot(
    IN nfs41_session *session,
    IN uint32_t slotid)
{
    nfs41_slot_table *table = &session->table;

    AcquireSRWLockShared(&session->client->session_lock);
    EnterCriticalSection(&table->lock);

    /* flag the slot as unused */
    if (slotid < NFS41_MAX_NUM_SLOTS && table->used_slots[slotid]) {
        table->used_slots[slotid] = 0;
        table->num_used--;
    }
    /* update highest_used if necessary */
    if (slotid == table->highest_used) {
        while (table->highest_used && !table->used_slots[table->highest_used])
            table->highest_used--;
    }
    dprintf(3, "freeing slot#=%d used=%d highest=%d\n",
        slotid, table->num_used, table->highest_used);

    /* wake any threads waiting on a slot */
    if (slot_table_avail(table))
        WakeAllConditionVariable(&table->cond);

    LeaveCriticalSection(&table->lock);
    ReleaseSRWLockShared(&session->client->session_lock);
}
Exemplo n.º 9
0
int pthread_rwlock_unlock(pthread_rwlock_t *l)
{
#ifndef PTHREAD_WIN_XP_SYNC
  void *state = *(void **)l;
	
  if (state == (void *) 1)
    {
      /* Known to be an exclusive lock */
      ReleaseSRWLockExclusive(l);
    }
  else
    {
      /* A shared unlock will work */
      ReleaseSRWLockShared(l);
    }
#else
  WaitForSingleObject(l->mutex,INFINITE);
  if(l->reader_count < 0)	// Known to be an exclusive lock 
    {
      l->reader_count = 0;
      if(l->nb_waiting_writer)	// writter have the priority
	ReleaseSemaphore(l->sema_write,1,NULL);	// Wakeup one writer
      else if(l->nb_waiting_reader)
	ReleaseSemaphore(l->sema_read,l->nb_waiting_reader,NULL); // Wake up all readers
    }
  else if(!--(l->reader_count) && l->nb_waiting_writer)	// maybe wake up one writer
    ReleaseSemaphore(l->sema_write,1,NULL);
  ReleaseMutex(l->mutex);
#endif
  return 0;
}
Exemplo n.º 10
0
int virtualfs_custom_stat(struct file *f, struct newstat *buf)
{
	AcquireSRWLockShared(&f->rw_lock);
	struct virtualfs_custom *file = (struct virtualfs_custom *)f;
	struct virtualfs_custom_desc *desc = (struct virtualfs_custom_desc *)file->desc;
	INIT_STRUCT_NEWSTAT_PADDING(buf);
	buf->st_dev = mkdev(0, 1);
	buf->st_ino = 0;
	buf->st_mode = S_IFCHR + 0644;
	buf->st_nlink = 1;
	buf->st_uid = 0;
	buf->st_gid = 0;
	buf->st_rdev = desc->device;
	buf->st_size = 0;
	buf->st_blksize = PAGE_SIZE;
	buf->st_blocks = 0;
	buf->st_atime = 0;
	buf->st_atime_nsec = 0;
	buf->st_mtime = 0;
	buf->st_mtime_nsec = 0;
	buf->st_ctime = 0;
	buf->st_ctime_nsec = 0;
	ReleaseSRWLockShared(&f->rw_lock);
	return 0;
}
Exemplo n.º 11
0
static int cl_exid_compare(
    IN const struct list_entry *entry,
    IN const void *value)
{
    nfs41_client *client = client_entry(entry);
    const struct cl_exid_info *info = (const struct cl_exid_info*)value;
    int status = ERROR_FILE_NOT_FOUND;

    AcquireSRWLockShared(&client->exid_lock);

    /* match any of the desired roles */
    if ((info->roles & client->roles) == 0)
        goto out;
    /* match server_owner.major_id */
    if (strncmp(info->exchangeid->server_owner.so_major_id,
        client->server->owner, NFS4_OPAQUE_LIMIT) != 0)
        goto out;
    /* match server_scope */
    if (strncmp(info->exchangeid->server_scope,
        client->server->scope, NFS4_OPAQUE_LIMIT) != 0)
        goto out;
    /* match clientid */
    if (info->exchangeid->clientid != client->clnt_id)
        goto out;

    status = NO_ERROR;
out:
    ReleaseSRWLockShared(&client->exid_lock);
    return status;
}
Exemplo n.º 12
0
static size_t winfs_write(struct file *f, const void *buf, size_t count)
{
	AcquireSRWLockShared(&f->rw_lock);
	struct winfs_file *winfile = (struct winfs_file *) f;
	WaitForSingleObject(winfile->fp_mutex, INFINITE);
	size_t num_written = 0;
	OVERLAPPED overlapped;
	overlapped.Internal = 0;
	overlapped.InternalHigh = 0;
	overlapped.Offset = 0xFFFFFFFF;
	overlapped.OffsetHigh = 0xFFFFFFFF;
	overlapped.hEvent = NULL;
	OVERLAPPED *overlapped_pointer = (f->flags & O_APPEND)? &overlapped: NULL;
	while (count > 0)
	{
		DWORD count_dword = (DWORD)min(count, (size_t)UINT_MAX);
		DWORD num_written_dword;
		if (!WriteFile(winfile->handle, buf, count_dword, &num_written_dword, overlapped_pointer))
		{
			log_warning("WriteFile() failed, error code: %d", GetLastError());
			num_written = -L_EIO;
			break;
		}
		num_written += num_written_dword;
		count -= num_written_dword;
	}
	ReleaseMutex(winfile->fp_mutex);
	ReleaseSRWLockShared(&f->rw_lock);
	return num_written;
}
BOOL acl_filter(PACL_LIST acl,const unsigned char HwType,const unsigned char* HwAddr,unsigned char HwAddrLen){
	AcquireSRWLockShared(&acl->lock);
	DEBUG("acl_filter: AcquireSRWLockShared()\n");
	__try{
		char buf[16*2+1];
		DEBUG("acl_filter(HwType=%d, HwAddr=%s)\n",(int)HwType,dhcp_hw_addr(buf,HwAddr,(int)HwAddrLen));
		unsigned int len = acl->length,i,j;
		PACL_ENTRY entries = acl->entry;
		for(i = 0;i<len;i++){
			DEBUG("acl_filter: entry[%d].HwType=%d arg.HwType=%d\n",i,(int)entries[i].HwType,(int)HwType);
			if(entries[i].HwType!=HwType)
				continue;
			
			for(j=0;j<HwAddrLen;j++){
				DEBUG("acl_filter: entry[%d].HwAddr[%d]=0x%02x arg.HwAddr[%d]=0x%02x\n",i,j,(int)entries[i].HwAddr[j],j,(int)HwAddr[j]);
				if((HwAddr[j]&entries[i].HwMask[j])!=entries[i].HwAddr[j]){
					goto next;
				}
			}
			return entries[i].access;
	next:	
			DEBUG("Goto Next\n");
			(void)0;
		}
		return TRUE;
	}__finally{
		ReleaseSRWLockShared(&acl->lock);
		DEBUG("acl_filter: ReleaseSRWLockShared()\n");
	}
}
Exemplo n.º 14
0
/// <summary>
/// Unlocks for reading (Call if thread acquired a read lock).
/// </summary>
void RWLock::UnlockRead()
{
#ifdef PLATFORM_WIN
	ReleaseSRWLockShared( &mRwlock );
#else
	pthread_rwlock_unlock( &mRwlock );
#endif
}
Exemplo n.º 15
0
void rwlock_rdunlock(rwlock_t *l)
{
#ifndef _WIN32
	atomic_ref(&l->s.write);
#else
	ReleaseSRWLockShared(l);
#endif
}
Exemplo n.º 16
0
static size_t virtualfs_char_write(struct file *f, const void *buf, size_t count)
{
	AcquireSRWLockShared(&f->rw_lock);
	struct virtualfs_char *file = (struct virtualfs_char *)f;
	size_t r = file->desc->write(file->tag, buf, count);
	ReleaseSRWLockShared(&f->rw_lock);
	return r;
}
Exemplo n.º 17
0
int mutex_unlock_shared(struct mutex_handle *mutex)
{
	struct mutex_priv *priv = (struct mutex_priv *)mutex->priv;

	ReleaseSRWLockShared(&priv->lock);

	return 0;
}
Exemplo n.º 18
0
	void shared_mutex::unlock() {
		bool ex = d->ex;
		if(ex) {
			d->ex=false;
			ReleaseSRWLockExclusive(&d->m);
		}
		else 
			ReleaseSRWLockShared(&d->m);
	}
Exemplo n.º 19
0
void
RWLock::ReadUnlockInternal()
{
#ifdef XP_WIN
  ReleaseSRWLockShared(NativeHandle(mRWLock));
#else
  MOZ_RELEASE_ASSERT(pthread_rwlock_unlock(NativeHandle(mRWLock)) == 0,
                     "pthread_rwlock_unlock failed");
#endif
}
Exemplo n.º 20
0
void RWLock::ReadUnlock()
{
#if defined(OVR_CAPTURE_WINDOWS)
    ReleaseSRWLockShared(&m_lock);
#elif defined(OVR_CAPTURE_POSIX)
    pthread_rwlock_unlock(&m_lock);
#else
#error Unknown Platform!
#endif
}
Exemplo n.º 21
0
Arquivo: winfs.c Projeto: xwlan/flinux
static int winfs_getpath(struct file *f, char *buf)
{
	AcquireSRWLockShared(&f->rw_lock);
	struct winfs_file *winfile = (struct winfs_file *)f;
	buf[0] = '/'; /* the mountpoint */
	memcpy(buf + 1, winfile->pathname, winfile->pathlen);
	buf[1 + winfile->pathlen] = 0;
	int r = winfile->pathlen + 1;
	ReleaseSRWLockShared(&f->rw_lock);
	return r;
}
Exemplo n.º 22
0
Arquivo: winfs.c Projeto: xwlan/flinux
static int winfs_stat(struct file *f, struct newstat *buf)
{
	AcquireSRWLockShared(&f->rw_lock);
	struct winfs_file *winfile = (struct winfs_file *) f;
	BY_HANDLE_FILE_INFORMATION info;
	GetFileInformationByHandle(winfile->handle, &info);

	/* Programs (ld.so) may use st_dev and st_ino to identity files so these must be unique for each file. */
	INIT_STRUCT_NEWSTAT_PADDING(buf);
	buf->st_dev = mkdev(8, 0); // (8, 0): /dev/sda
	//buf->st_ino = ((uint64_t)info.nFileIndexHigh << 32ULL) + info.nFileIndexLow;
	/* Hash 64 bit inode to 32 bit to fix legacy applications
	 * We may later add an option for changing this behaviour
	 */
	buf->st_ino = info.nFileIndexHigh ^ info.nFileIndexLow;
	if (info.dwFileAttributes & FILE_ATTRIBUTE_READONLY)
		buf->st_mode = 0555;
	else
		buf->st_mode = 0755;
	if (info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
	{
		buf->st_mode |= S_IFDIR;
		buf->st_size = 0;
	}
	else
	{
		int r;
		if ((info.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM)
			&& (r = winfs_read_symlink_unsafe(winfile->handle, NULL, 0)) > 0)
		{
			buf->st_mode |= S_IFLNK;
			buf->st_size = r;
		}
		else
		{
			buf->st_mode |= S_IFREG;
			buf->st_size = ((uint64_t)info.nFileSizeHigh << 32ULL) + info.nFileSizeLow;
		}
	}
	buf->st_nlink = info.nNumberOfLinks;
	buf->st_uid = 0;
	buf->st_gid = 0;
	buf->st_rdev = 0;
	buf->st_blksize = PAGE_SIZE;
	buf->st_blocks = (buf->st_size + buf->st_blksize - 1) / buf->st_blksize;
	buf->st_atime = filetime_to_unix_sec(&info.ftLastAccessTime);
	buf->st_atime_nsec = filetime_to_unix_nsec(&info.ftLastAccessTime);
	buf->st_mtime = filetime_to_unix_sec(&info.ftLastWriteTime);
	buf->st_mtime_nsec = filetime_to_unix_nsec(&info.ftLastWriteTime);
	buf->st_ctime = filetime_to_unix_sec(&info.ftCreationTime);
	buf->st_ctime_nsec = filetime_to_unix_nsec(&info.ftCreationTime);
	ReleaseSRWLockShared(&f->rw_lock);
	return 0;
}
Exemplo n.º 23
0
int
pthread_rwlock_unlock(pthread_rwlock_t *rwlock)
{
	if (rwlock->exclusive_locked != 0) {
		rwlock->exclusive_locked = 0;
		ReleaseSRWLockExclusive(&rwlock->rwlock);
	} else
		ReleaseSRWLockShared(&rwlock->rwlock);

	return (0);
}
Exemplo n.º 24
0
Arquivo: dirbuf.c Projeto: os12/winfsp
FSP_API VOID FspFileSystemReadDirectoryBuffer(PVOID *PDirBuffer,
    PWSTR Marker,
    PVOID Buffer, ULONG Length, PULONG PBytesTransferred)
{
    FSP_FILE_SYSTEM_DIRECTORY_BUFFER *DirBuffer = *PDirBuffer;
    MemoryBarrier();

    if (0 != DirBuffer)
    {
        AcquireSRWLockShared(&DirBuffer->Lock);

        PULONG Index = (PULONG)(DirBuffer->Buffer + DirBuffer->HiMark);
        ULONG Count = (DirBuffer->Capacity - DirBuffer->HiMark) / sizeof(ULONG);
        ULONG IndexNum;
        FSP_FSCTL_DIR_INFO *DirInfo;

        if (0 == Marker)
            IndexNum = 0;
        else
        {
            FspFileSystemSearchDirectoryBuffer(DirBuffer,
                Marker, lstrlenW(Marker),
                &IndexNum);
            IndexNum++;
        }

        for (; IndexNum < Count; IndexNum++)
        {
            DirInfo = (PVOID)(DirBuffer->Buffer + Index[IndexNum]);
            if (!FspFileSystemAddDirInfo(DirInfo, Buffer, Length, PBytesTransferred))
            {
                ReleaseSRWLockShared(&DirBuffer->Lock);
                return;
            }
        }

        ReleaseSRWLockShared(&DirBuffer->Lock);
    }

    FspFileSystemAddDirInfo(0, Buffer, Length, PBytesTransferred);
}
Exemplo n.º 25
0
 _PPLXIMP void reader_writer_lock_impl::unlock()
 {
     if(m_locked_exclusive)
     {
         m_locked_exclusive = false;
         ReleaseSRWLockExclusive(reinterpret_cast<PSRWLOCK>(&_M_impl));
     }
     else
     {
         ReleaseSRWLockShared(reinterpret_cast<PSRWLOCK>(&_M_impl));
     }
 }
Exemplo n.º 26
0
static int winfs_fsync(struct file *f)
{
	AcquireSRWLockShared(&f->rw_lock);
	struct winfs_file *winfile = (struct winfs_file *) f;
	BOOL ok = FlushFileBuffers(winfile->handle);
	ReleaseSRWLockShared(&f->rw_lock);
	if (!ok)
	{
		log_warning("FlushFileBuffers() failed, error code: %d", GetLastError());
		return -L_EIO;
	}
	return 0;
}
Exemplo n.º 27
0
void
CPacBioUtility::ReleaseLock(bool bExclusive)
{

#ifdef _WIN32
if(bExclusive)
	ReleaseSRWLockExclusive(&m_hRwLock);
else
	ReleaseSRWLockShared(&m_hRwLock);
#else
pthread_rwlock_unlock(&m_hRwLock);
#endif
}
Exemplo n.º 28
0
	void ui::update()
	{
		field.update();
		if (TryAcquireSRWLockShared(&uilock))
		{
			for (map<char, uielement*>::iterator elit = elements.begin(); elit != elements.end(); elit++)
			{
				elit->second->update();
			}
			ReleaseSRWLockShared(&uilock);
		}
		mouse.update();
	}
Exemplo n.º 29
0
	void ui::draw(ID2D1HwndRenderTarget* target)
	{
		field.draw(target);
		if (TryAcquireSRWLockShared(&uilock))
		{
			for (map<char, uielement*>::iterator elit = elements.begin(); elit != elements.end(); elit++)
			{
				elit->second->draw(target);
			}
			ReleaseSRWLockShared(&uilock);
		}
		mouse.draw(target);
	}
Exemplo n.º 30
0
ts_tree_node_t* ts_tree_find_and_ref(ts_tree_t* ts_tree, uintptr_t key) {
  ts_tree_node_t* ts_tree_node;

  AcquireSRWLockShared(&ts_tree->lock);

  ts_tree_node = ts_tree__find_node(ts_tree, key);
  if (ts_tree_node != NULL)
    reflock_ref(&ts_tree_node->reflock);

  ReleaseSRWLockShared(&ts_tree->lock);

  return ts_tree_node;
}