thread.c 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*
  2. * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
  3. * Copyright (C) 2004-2005 Red Hat, Inc. All rights reserved.
  4. *
  5. * This copyrighted material is made available to anyone wishing to use,
  6. * modify, copy, or redistribute it subject to the terms and conditions
  7. * of the GNU General Public License version 2.
  8. */
  9. #include "lock_dlm.h"
  10. static inline int no_work(struct gdlm_ls *ls)
  11. {
  12. int ret;
  13. spin_lock(&ls->async_lock);
  14. ret = list_empty(&ls->submit);
  15. spin_unlock(&ls->async_lock);
  16. return ret;
  17. }
  18. static int gdlm_thread(void *data)
  19. {
  20. struct gdlm_ls *ls = (struct gdlm_ls *) data;
  21. struct gdlm_lock *lp = NULL;
  22. while (!kthread_should_stop()) {
  23. wait_event_interruptible(ls->thread_wait,
  24. !no_work(ls) || kthread_should_stop());
  25. spin_lock(&ls->async_lock);
  26. if (!list_empty(&ls->submit)) {
  27. lp = list_entry(ls->submit.next, struct gdlm_lock,
  28. delay_list);
  29. list_del_init(&lp->delay_list);
  30. spin_unlock(&ls->async_lock);
  31. gdlm_do_lock(lp);
  32. spin_lock(&ls->async_lock);
  33. }
  34. spin_unlock(&ls->async_lock);
  35. }
  36. return 0;
  37. }
  38. int gdlm_init_threads(struct gdlm_ls *ls)
  39. {
  40. struct task_struct *p;
  41. int error;
  42. p = kthread_run(gdlm_thread, ls, "lock_dlm");
  43. error = IS_ERR(p);
  44. if (error) {
  45. log_error("can't start lock_dlm thread %d", error);
  46. return error;
  47. }
  48. ls->thread = p;
  49. return 0;
  50. }
  51. void gdlm_release_threads(struct gdlm_ls *ls)
  52. {
  53. kthread_stop(ls->thread);
  54. }