4

I need to implement Oplock (opportunistic locking) in .NET project. Here is the scenario that I need to implement:

  1. App 1 opens the file and does NOT allow writing (for example, opens a file with FileShare.Read).
  2. App 2 requests writing to the file (opens a file with FileAccess.Write).
  3. App 1 receives a notification that the writing is requested. It confirms breaking the lock and allows App 2 to write to the file.

There is a full asynchronous I/O support in the FileStream class and there is also NativeOverlapped structure in .NET. Looks like .NET has everything needed for Oplock. However there is no any example how to use NativeOverlapped and NativeOverlapped.EventHandle field required to handle the oplock break and no any parameters for this structure.

Is Oplock is supported in .NET at all? Are there any Oplock example?

Theodor Zoulias
  • 34,835
  • 7
  • 69
  • 104
user16668952
  • 184
  • 4
  • 1
    but this is already implemeented by windows. you simply do `FSCTL_REQUEST_OPLOCK` in App1 with `OPLOCK_LEVEL_CACHE_READ` and `REQUEST_OPLOCK_INPUT_FLAG_REQUEST` and got status pending for this. when App2 writing to the file the `FSCTL_REQUEST_OPLOCK` will be completed – RbMm Oct 24 '21 at 10:07
  • 1
    @Charlieface - you mistake. winapi full support this ( `FSCTL_REQUEST_OPLOCK`) not need any loops – RbMm Oct 24 '21 at 10:18
  • The .NET Framework has `ReaderWriterLock` and `ReaderWriterLockSlim` that implement the concept of multiple readers-one writer for a lock. This is not file-access specific but of course can be used to control access to a file. – PMF Oct 24 '21 at 11:53
  • @PMF that is only per process and nothing common with file oplock – RbMm Oct 24 '21 at 11:56
  • @RbMm True, I didn't see that this was really about file access. I thought the OP was asking primarily about the _concept_. – PMF Oct 24 '21 at 12:01
  • 2
    @PMF - i think here main concept that App 2 have no knowledge about App 1 and not use any common synchronization with it – RbMm Oct 24 '21 at 12:17

0 Answers0