-
-/*
- * Release any Volume attached to this device
- * then close the device.
- */
-void close_device(DEVICE *dev)
-{
- free_volume(dev);
- dev->close();
-}
-
-/*
- * Used when unmounting the device, ignore use_count
- */
-void force_close_device(DEVICE *dev)
-{
- if (!dev) {
- Mmsg0(dev->errmsg, _("Bad call to force_close_dev. Device not open\n"));
- Emsg0(M_FATAL, 0, dev->errmsg);
- return;
- }
- Dmsg1(29, "Force close_dev %s\n", dev->print_name());
- dev->use_count = 1;
- dev->close();
-
-#ifdef FULL_DEBUG
- ASSERT(dev->use_count >= 0);
-#endif
-}
-
-
-void dev_lock(DEVICE *dev)
-{
- int errstat;
- if ((errstat=rwl_writelock(&dev->lock))) {
- Emsg1(M_ABORT, 0, "Device write lock failure. ERR=%s\n", strerror(errstat));
- }
-}
-
-void dev_unlock(DEVICE *dev)
-{
- int errstat;
- if ((errstat=rwl_writeunlock(&dev->lock))) {
- Emsg1(M_ABORT, 0, "Device write unlock failure. ERR=%s\n", strerror(errstat));
- }
-}
-
-/*
- * When dev_blocked is set, all threads EXCEPT thread with id no_wait_id
- * must wait. The no_wait_id thread is out obtaining a new volume
- * and preparing the label.
- */
-void _lock_device(const char *file, int line, DEVICE *dev)
-{
- int stat;
- Dmsg3(500, "lock %d from %s:%d\n", dev->dev_blocked, file, line);
- P(dev->mutex);
- if (dev->dev_blocked && !pthread_equal(dev->no_wait_id, pthread_self())) {
- dev->num_waiting++; /* indicate that I am waiting */
- while (dev->dev_blocked) {
- if ((stat = pthread_cond_wait(&dev->wait, &dev->mutex)) != 0) {
- V(dev->mutex);
- Emsg1(M_ABORT, 0, _("pthread_cond_wait failure. ERR=%s\n"),
- strerror(stat));
- }
- }
- dev->num_waiting--; /* no longer waiting */
- }
-}
-
-/*
- * Check if the device is blocked or not
- */
-bool is_device_unmounted(DEVICE *dev)
-{
- bool stat;
- int blocked = dev->dev_blocked;
- stat = (blocked == BST_UNMOUNTED) ||
- (blocked == BST_UNMOUNTED_WAITING_FOR_SYSOP);
- return stat;
-}
-
-void _unlock_device(const char *file, int line, DEVICE *dev)
-{
- Dmsg2(500, "unlock from %s:%d\n", file, line);
- V(dev->mutex);
-}
-
-/*
- * Block all other threads from using the device
- * Device must already be locked. After this call,
- * the device is blocked to any thread calling lock_device(),
- * but the device is not locked (i.e. no P on device). Also,
- * the current thread can do slip through the lock_device()
- * calls without blocking.
- */
-void _block_device(const char *file, int line, DEVICE *dev, int state)
-{
- Dmsg3(500, "block set %d from %s:%d\n", state, file, line);
- ASSERT(dev->get_blocked() == BST_NOT_BLOCKED);
- dev->set_blocked(state); /* make other threads wait */
- dev->no_wait_id = pthread_self(); /* allow us to continue */
-}
-
-
-
-/*
- * Unblock the device, and wake up anyone who went to sleep.
- */
-void _unblock_device(const char *file, int line, DEVICE *dev)
-{
- Dmsg3(500, "unblock %s from %s:%d\n", dev->print_blocked(), file, line);
- ASSERT(dev->dev_blocked);
- dev->set_blocked(BST_NOT_BLOCKED);
- dev->no_wait_id = 0;
- if (dev->num_waiting > 0) {
- pthread_cond_broadcast(&dev->wait); /* wake them up */
- }
-}
-
-/*
- * Enter with device locked and blocked
- * Exit with device unlocked and blocked by us.
- */
-void _steal_device_lock(const char *file, int line, DEVICE *dev, bsteal_lock_t *hold, int state)
-{
-
- Dmsg3(400, "steal lock. old=%s from %s:%d\n", dev->print_blocked(),
- file, line);
- hold->dev_blocked = dev->get_blocked();
- hold->dev_prev_blocked = dev->dev_prev_blocked;
- hold->no_wait_id = dev->no_wait_id;
- dev->set_blocked(state);
- Dmsg1(400, "steal lock. new=%s\n", dev->print_blocked());
- dev->no_wait_id = pthread_self();
- V(dev->mutex);
-}
-
-/*
- * Enter with device blocked by us but not locked
- * Exit with device locked, and blocked by previous owner
- */
-void _give_back_device_lock(const char *file, int line, DEVICE *dev, bsteal_lock_t *hold)
-{
- Dmsg3(400, "return lock. old=%s from %s:%d\n",
- dev->print_blocked(), file, line);
- P(dev->mutex);
- dev->dev_blocked = hold->dev_blocked;
- dev->dev_prev_blocked = hold->dev_prev_blocked;
- dev->no_wait_id = hold->no_wait_id;
- Dmsg1(400, "return lock. new=%s\n", dev->print_blocked());
- if (dev->num_waiting > 0) {
- Dmsg0(400, "Broadcast\n");
- pthread_cond_broadcast(&dev->wait); /* wake them up */
- }
-}