upload_pipeline_lock.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package page_writer
  2. import (
  3. "sync/atomic"
  4. )
  5. func (up *UploadPipeline) LockForRead(startOffset, stopOffset int64) {
  6. startLogicChunkIndex := LogicChunkIndex(startOffset / up.ChunkSize)
  7. stopLogicChunkIndex := LogicChunkIndex(stopOffset / up.ChunkSize)
  8. if stopOffset%up.ChunkSize > 0 {
  9. stopLogicChunkIndex += 1
  10. }
  11. up.activeReadChunksLock.Lock()
  12. defer up.activeReadChunksLock.Unlock()
  13. for i := startLogicChunkIndex; i < stopLogicChunkIndex; i++ {
  14. if count, found := up.activeReadChunks[i]; found {
  15. up.activeReadChunks[i] = count + 1
  16. } else {
  17. up.activeReadChunks[i] = 1
  18. }
  19. }
  20. }
  21. func (up *UploadPipeline) UnlockForRead(startOffset, stopOffset int64) {
  22. startLogicChunkIndex := LogicChunkIndex(startOffset / up.ChunkSize)
  23. stopLogicChunkIndex := LogicChunkIndex(stopOffset / up.ChunkSize)
  24. if stopOffset%up.ChunkSize > 0 {
  25. stopLogicChunkIndex += 1
  26. }
  27. up.activeReadChunksLock.Lock()
  28. defer up.activeReadChunksLock.Unlock()
  29. for i := startLogicChunkIndex; i < stopLogicChunkIndex; i++ {
  30. if count, found := up.activeReadChunks[i]; found {
  31. if count == 1 {
  32. delete(up.activeReadChunks, i)
  33. } else {
  34. up.activeReadChunks[i] = count - 1
  35. }
  36. }
  37. }
  38. }
  39. func (up *UploadPipeline) IsLocked(logicChunkIndex LogicChunkIndex) bool {
  40. up.activeReadChunksLock.Lock()
  41. defer up.activeReadChunksLock.Unlock()
  42. if count, found := up.activeReadChunks[logicChunkIndex]; found {
  43. return count > 0
  44. }
  45. return false
  46. }
  47. func (up *UploadPipeline) waitForCurrentWritersToComplete() {
  48. up.uploaderCountCond.L.Lock()
  49. t := int32(100)
  50. for {
  51. t = atomic.LoadInt32(&up.uploaderCount)
  52. if t <= 0 {
  53. break
  54. }
  55. up.uploaderCountCond.Wait()
  56. }
  57. up.uploaderCountCond.L.Unlock()
  58. }