Ticket #2282: 2282-v3.patch
File 2282-v3.patch, 19.2 KB (added by , 18 years ago) |
---|
-
programs/mythbackend/scheduler.cpp
76 76 } 77 77 } 78 78 79 Scheduler::~Scheduler()79 static void delete_reclist(RecList &reclist) 80 80 { 81 81 while (reclist.size() > 0) 82 82 { … … 86 86 } 87 87 } 88 88 89 static void copy_reclist(RecList ©, const RecList &orig) 90 { 91 delete_reclist(copy); 92 93 RecList::const_iterator it = orig.begin(); 94 for (; it != orig.end(); it++) 95 copy.push_back(new ProgramInfo(*(*it))); 96 } 97 98 Scheduler::~Scheduler() 99 { 100 delete_reclist(reclist); 101 102 if (reclist_lock) 103 { 104 delete reclist_lock; 105 reclist_lock = NULL; 106 } 107 108 if (schedlist_lock) 109 { 110 delete schedlist_lock; 111 schedlist_lock = NULL; 112 } 113 } 114 89 115 void Scheduler::SetMainServer(MainServer *ms) 90 116 { 91 117 m_mainServer = ms; … … 263 289 264 290 bool Scheduler::FillRecordList(void) 265 291 { 266 QMutexLocker lockit(reclist_lock);267 268 292 schedMoveHigher = (bool)gContext->GetNumSetting("SchedMoveHigher"); 269 293 schedTime = QDateTime::currentDateTime(); 270 294 271 VERBOSE(VB_SCHEDULE, "PruneOldRecords..."); 272 PruneOldRecords(); 273 VERBOSE(VB_SCHEDULE, "AddNewRecords..."); 274 AddNewRecords(); 275 VERBOSE(VB_SCHEDULE, "AddNotListed..."); 276 AddNotListed(); 295 RecList newlist; 277 296 297 PruneOldRecords(newlist); 298 AddNewRecords(dbConn, recordTable, specsched, threadrunning, 299 *m_tvList, newlist); 300 AddNotListed(dbConn, recordTable, newlist); 301 278 302 VERBOSE(VB_SCHEDULE, "Sort by time..."); 279 reclist.sort(comp_overlap); 280 VERBOSE(VB_SCHEDULE, "PruneOverlaps..."); 281 PruneOverlaps(); 303 newlist.sort(comp_overlap); 282 304 305 PruneOverlaps(newlist); 306 283 307 VERBOSE(VB_SCHEDULE, "Sort by priority..."); 284 reclist.sort(comp_priority); 285 VERBOSE(VB_SCHEDULE, "BuildListMaps..."); 286 BuildListMaps(); 287 VERBOSE(VB_SCHEDULE, "SchedNewRecords..."); 288 SchedNewRecords(); 289 VERBOSE(VB_SCHEDULE, "ClearListMaps..."); 290 ClearListMaps(); 308 newlist.sort(comp_priority); 291 309 310 { 311 QMap<int, RecList> cardlistmap; 312 QMap<int, RecList> recordidlistmap; 313 QMap<QString, RecList> titlelistmap; 314 315 BuildListMaps(newlist, cardlistmap, recordidlistmap, titlelistmap); 316 317 RecList retrylist; 318 SchedNewRecords(schedMoveHigher, newlist, retrylist, 319 cardlistmap, recordidlistmap, titlelistmap); 320 } 321 292 322 VERBOSE(VB_SCHEDULE, "Sort by time..."); 293 reclist.sort(comp_redundant); 294 VERBOSE(VB_SCHEDULE, "PruneRedundants..."); 295 PruneRedundants(); 323 newlist.sort(comp_redundant); 296 324 325 bool will_have_conflicts = PruneRedundants(newlist); 326 297 327 VERBOSE(VB_SCHEDULE, "Sort by time..."); 298 reclist.sort(comp_recstart);328 newlist.sort(comp_recstart); 299 329 300 VERBOSE(VB_SCHEDULE, "UpdateNextRecord..."); 301 UpdateNextRecord(); 330 UpdateNextRecord(dbConn, newlist); 302 331 332 VERBOSE(VB_SCHEDULE, "Scheduler::FillRecordList() -- locking"); 333 reclist_lock->lock(); 334 VERBOSE(VB_SCHEDULE, "Scheduler::FillRecordList() -- locked"); 335 336 copy_reclist(reclist, newlist); 337 hasconflicts = will_have_conflicts; 338 339 reclist_lock->unlock(); 340 VERBOSE(VB_SCHEDULE, "Scheduler::FillRecordList() -- unlocked"); 341 342 delete_reclist(newlist); 343 303 344 return hasconflicts; 304 345 } 305 346 … … 479 520 } 480 521 } 481 522 482 bool Scheduler::ChangeRecordingEnd(ProgramInfo *oldp, ProgramInfo *newp) 523 bool Scheduler::ChangeRecordingEnd(const bool specsched, 524 QMap<int, EncoderLink*> &tvList, 525 ProgramInfo *oldp, 526 ProgramInfo *newp) 483 527 { 484 528 RecordingType oldrectype = oldp->rectype; 485 529 int oldrecordid = oldp->recordid; … … 500 544 else 501 545 return true; 502 546 } 503 EncoderLink *tv = (*m_tvList)[oldp->cardid];547 EncoderLink *tv = tvList[oldp->cardid]; 504 548 RecStatusType rs = tv->StartRecording(oldp); 505 549 if (rs != rsRecording) 506 550 { … … 590 634 } 591 635 } 592 636 593 void Scheduler::PruneOldRecords( void)637 void Scheduler::PruneOldRecords(RecList &reclist) 594 638 { 639 VERBOSE(VB_SCHEDULE, "PruneOldRecords..."); 595 640 RecIter dreciter = reclist.begin(); 596 641 while (dreciter != reclist.end()) 597 642 { … … 610 655 } 611 656 } 612 657 613 void Scheduler::PruneOverlaps( void)658 void Scheduler::PruneOverlaps(RecList &reclist) 614 659 { 660 VERBOSE(VB_SCHEDULE, "PruneOverlaps..."); 615 661 ProgramInfo *lastp = NULL; 616 662 617 663 RecIter dreciter = reclist.begin(); … … 651 697 } 652 698 } 653 699 654 void Scheduler::BuildListMaps(void) 700 void Scheduler::BuildListMaps(RecList &reclist, 701 QMap<int, RecList> &cardlistmap, 702 QMap<int, RecList> &recordidlistmap, 703 QMap<QString, RecList> &titlelistmap) 655 704 { 705 VERBOSE(VB_SCHEDULE, "BuildListMaps..."); 656 706 RecIter i = reclist.begin(); 657 707 for ( ; i != reclist.end(); i++) 658 708 { … … 668 718 } 669 719 } 670 720 671 void Scheduler::ClearListMaps(void)672 {673 cardlistmap.clear();674 titlelistmap.clear();675 recordidlistmap.clear();676 }677 678 721 bool Scheduler::FindNextConflict(RecList &cardlist, ProgramInfo *p, RecIter &j) 679 722 { 680 723 for ( ; j != cardlist.end(); j++) … … 698 741 return false; 699 742 } 700 743 701 void Scheduler::MarkOtherShowings(ProgramInfo *p) 744 void Scheduler::MarkOtherShowings(ProgramInfo *p, 745 QMap<int, RecList> &recordidlistmap, 746 QMap<QString, RecList> &titlelistmap) 702 747 { 703 748 RecList *showinglist = &titlelistmap[p->title]; 704 749 … … 746 791 } 747 792 } 748 793 749 void Scheduler::BackupRecStatus( void)794 void Scheduler::BackupRecStatus(RecList &reclist) 750 795 { 751 796 RecIter i = reclist.begin(); 752 797 for ( ; i != reclist.end(); i++) … … 756 801 } 757 802 } 758 803 759 void Scheduler::RestoreRecStatus( void)804 void Scheduler::RestoreRecStatus(RecList &reclist) 760 805 { 761 806 RecIter i = reclist.begin(); 762 807 for ( ; i != reclist.end(); i++) … … 766 811 } 767 812 } 768 813 769 bool Scheduler::TryAnotherShowing(ProgramInfo *p) 814 bool Scheduler::TryAnotherShowing(QMap<int, RecList> &cardlistmap, 815 QMap<QString, RecList> &titlelistmap, 816 QMap<int, RecList> &recordidlistmap, 817 ProgramInfo *p) 770 818 { 771 819 PrintRec(p, " >"); 772 820 … … 815 863 } 816 864 817 865 q->recstatus = rsWillRecord; 818 MarkOtherShowings(q );866 MarkOtherShowings(q, recordidlistmap, titlelistmap); 819 867 PrintRec(p, " -"); 820 868 PrintRec(q, " +"); 821 869 return true; … … 825 873 return false; 826 874 } 827 875 828 void Scheduler::SchedNewRecords(void) 876 void Scheduler::SchedNewRecords(const bool schedMoveHigher, 877 RecList &reclist, 878 RecList &retrylist, 879 QMap<int, RecList> &cardlistmap, 880 QMap<int, RecList> &recordidlistmap, 881 QMap<QString, RecList> &titlelistmap) 829 882 { 830 883 VERBOSE(VB_SCHEDULE, "Scheduling:"); 831 884 … … 834 887 { 835 888 ProgramInfo *p = *i; 836 889 if (p->recstatus == rsRecording) 837 MarkOtherShowings(p );890 MarkOtherShowings(p, recordidlistmap, titlelistmap); 838 891 else if (p->recstatus == rsUnknown) 839 892 { 840 893 RecList &cardlist = cardlistmap[p->cardid]; … … 842 895 if (!FindNextConflict(cardlist, p, k)) 843 896 { 844 897 p->recstatus = rsWillRecord; 845 MarkOtherShowings(p );898 MarkOtherShowings(p, recordidlistmap, titlelistmap); 846 899 PrintRec(p, " +"); 847 900 } 848 901 else … … 857 910 i++; 858 911 if (i == reclist.end() || lastpri != (*i)->recpriority) 859 912 { 860 MoveHigherRecords(); 913 MoveHigherRecords(schedMoveHigher, reclist, retrylist, 914 cardlistmap, recordidlistmap, titlelistmap); 861 915 retrylist.clear(); 862 916 } 863 917 } 864 918 } 865 919 866 void Scheduler::MoveHigherRecords(void) 920 void Scheduler::MoveHigherRecords(const bool schedMoveHigher, 921 RecList &reclist, 922 RecList &retrylist, 923 QMap<int, RecList> &cardlistmap, 924 QMap<int, RecList> &recordidlistmap, 925 QMap<QString, RecList> &titlelistmap) 867 926 { 868 927 RecIter i = retrylist.begin(); 869 928 for ( ; i != retrylist.end(); i++) … … 874 933 875 934 PrintRec(p, " ?"); 876 935 877 if (TryAnotherShowing( p))936 if (TryAnotherShowing(cardlistmap, titlelistmap, recordidlistmap, p)) 878 937 continue; 879 938 880 BackupRecStatus( );939 BackupRecStatus(reclist); 881 940 p->recstatus = rsWillRecord; 882 MarkOtherShowings(p );941 MarkOtherShowings(p, recordidlistmap, titlelistmap); 883 942 884 943 RecList &cardlist = cardlistmap[p->cardid]; 885 944 RecIter k = cardlist.begin(); 886 945 for ( ; FindNextConflict(cardlist, p, k); k++) 887 946 { 888 947 if ((p->recpriority < (*k)->recpriority && !schedMoveHigher) || 889 !TryAnotherShowing(*k)) 948 !TryAnotherShowing(cardlistmap, titlelistmap, 949 recordidlistmap, *k)) 890 950 { 891 RestoreRecStatus( );951 RestoreRecStatus(reclist); 892 952 break; 893 953 } 894 954 } … … 898 958 } 899 959 } 900 960 901 void Scheduler::PruneRedundants(void)961 bool Scheduler::PruneRedundants(RecList &reclist) 902 962 { 963 VERBOSE(VB_SCHEDULE, "PruneRedundants..."); 903 964 ProgramInfo *lastp = NULL; 904 hasconflicts = false;965 bool hasconflicts = false; 905 966 906 967 RecIter i = reclist.begin(); 907 968 while (i != reclist.end()) … … 951 1012 i = reclist.erase(i); 952 1013 } 953 1014 } 1015 1016 return hasconflicts; 954 1017 } 955 1018 956 void Scheduler::UpdateNextRecord(void) 1019 void Scheduler::UpdateNextRecord(MSqlQueryInfo &dbConn, 1020 RecList &reclist) 957 1021 { 1022 VERBOSE(VB_SCHEDULE, "UpdateNextRecord..."); 958 1023 QMap<int, QDateTime> nextRecMap; 959 1024 QDateTime now = QDateTime::currentDateTime(); 960 1025 … … 1043 1108 { 1044 1109 QMutexLocker lockit(reclist_lock); 1045 1110 1046 while (retList->size() > 0) 1047 { 1048 ProgramInfo *pginfo = retList->back(); 1049 delete pginfo; 1050 retList->pop_back(); 1051 } 1052 1053 RecIter i = reclist.begin(); 1054 for (; i != reclist.end(); i++) 1055 { 1056 ProgramInfo *p = *i; 1057 retList->push_back(new ProgramInfo(*p)); 1058 } 1111 copy_reclist(*retList, reclist); 1059 1112 retList->sort(comp_timechannel); 1060 1113 } 1061 1114 1062 1115 void Scheduler::getAllPending(QStringList &strList) 1063 1116 { 1064 QMutexLocker lockit(reclist_lock); 1117 RecList retList; 1118 getAllPending(&retList); 1065 1119 1066 1120 strList << QString::number(hasconflicts); 1067 strList << QString::number(re clist.size());1121 strList << QString::number(retList.size()); 1068 1122 1069 RecList *retList = new RecList; 1123 for (RecIter it = retList.begin(); it != retList.end(); ++it) 1124 (*it)->ToStringList(strList); 1070 1125 1071 RecIter i = reclist.begin(); 1072 for (; i != reclist.end(); i++) 1073 { 1074 ProgramInfo *p = *i; 1075 retList->push_back(new ProgramInfo(*p)); 1076 } 1077 retList->sort(comp_timechannel); 1078 1079 for (i = retList->begin(); i != retList->end(); i++) 1080 { 1081 ProgramInfo *p = *i; 1082 p->ToStringList(strList); 1083 delete p; 1084 } 1085 1086 delete retList; 1126 delete_reclist(retList); 1087 1127 } 1088 1128 1089 1129 RecList *Scheduler::getAllScheduled(void) 1090 1130 { 1091 while (schedlist.size() > 0) 1092 { 1093 ProgramInfo *pginfo = schedlist.back(); 1094 delete pginfo; 1095 schedlist.pop_back(); 1096 } 1131 delete_reclist(schedlist); 1097 1132 1098 1133 findAllScheduledPrograms(schedlist); 1099 1134 … … 2008 2043 VERBOSE(VB_SCHEDULE, " +-- Done."); 2009 2044 } 2010 2045 2011 void Scheduler::AddNewRecords(void) 2046 void Scheduler::AddNewRecords(MSqlQueryInfo &dbConn, 2047 const QString &recordTable, 2048 const bool specsched, 2049 const bool threadrunning, 2050 QMap<int, EncoderLink*> &tvList, 2051 RecList &reclist) 2012 2052 { 2053 VERBOSE(VB_SCHEDULE, "AddNewRecords..."); 2013 2054 struct timeval dbstart, dbend; 2014 2055 2015 2056 QMap<RecordingType, int> recTypeRecPriorityMap; … … 2022 2063 int ccpriority = gContext->GetNumSetting("CCRecPriority", 0); 2023 2064 2024 2065 QMap<int, bool> cardMap; 2025 QMap<int, EncoderLink *>::Iterator enciter = m_tvList->begin();2026 for (; enciter != m_tvList->end(); ++enciter)2066 QMap<int, EncoderLink *>::Iterator enciter = tvList.begin(); 2067 for (; enciter != tvList.end(); ++enciter) 2027 2068 { 2028 2069 EncoderLink *enc = enciter.data(); 2029 2070 if (enc->IsConnected()) … … 2333 2374 r->recendts != p->recendts && 2334 2375 (r->recordid == p->recordid || 2335 2376 p->rectype == kOverrideRecord)) 2336 ChangeRecordingEnd(r, p); 2377 { 2378 ChangeRecordingEnd(specsched, tvList, r, p); 2379 } 2337 2380 delete p; 2338 2381 p = NULL; 2339 2382 break; … … 2390 2433 reclist.push_back(*tmp); 2391 2434 } 2392 2435 2393 void Scheduler::AddNotListed(void) { 2394 2436 void Scheduler::AddNotListed(MSqlQueryInfo &dbConn, 2437 const QString &recordTable, 2438 RecList &reclist) 2439 { 2440 VERBOSE(VB_SCHEDULE, "AddNotListed..."); 2395 2441 struct timeval dbstart, dbend; 2396 2442 RecList tmpList; 2397 2443 -
programs/mythbackend/scheduler.h
49 49 RecList *getConflicting(ProgramInfo *pginfo); 50 50 51 51 void PrintList(bool onlyFutureRecordings = false); 52 void PrintRec(ProgramInfo *p, const char *prefix = NULL);52 static void PrintRec(ProgramInfo *p, const char *prefix = NULL); 53 53 54 54 bool HasConflicts(void) { return hasconflicts; } 55 55 … … 66 66 static void *SchedulerThread(void *param); 67 67 68 68 private: 69 QString recordTable;70 71 69 void verifyCards(void); 72 70 73 71 bool FillRecordList(void); 74 72 void UpdateMatches(int recordid); 75 73 void UpdateManuals(int recordid); 76 void PruneOldRecords(void);77 void AddNewRecords(void);78 void AddNotListed(void);79 74 void BuildNewRecordsQueries(int recordid, QStringList &from, QStringList &where, 80 75 MSqlBindings &bindings); 81 void PruneOverlaps(void);82 void BuildListMaps(void);83 void ClearListMaps(void);84 bool FindNextConflict(RecList &cardlist, ProgramInfo *p, RecIter &iter);85 void MarkOtherShowings(ProgramInfo *p);86 void MarkShowingsList(RecList &showinglist, ProgramInfo *p);87 void BackupRecStatus(void);88 void RestoreRecStatus(void);89 bool TryAnotherShowing(ProgramInfo *p);90 void SchedNewRecords(void);91 void MoveHigherRecords(void);92 void PruneRedundants(void);93 void UpdateNextRecord(void);94 76 95 bool ChangeRecordingEnd(ProgramInfo *oldp, ProgramInfo *newp);96 97 77 void findAllScheduledPrograms(list<ProgramInfo *> &proglist); 98 78 bool CheckShutdownServer(int prerollseconds, QDateTime &idleSince, 99 79 bool &blockShutdown); 100 80 void ShutdownServer(int prerollseconds); 101 81 82 // statics 83 static void PruneOldRecords(RecList &reclist); 84 static void AddNewRecords(MSqlQueryInfo &dbConn, 85 const QString &recordTable, 86 bool specsched, 87 bool threadrunning, 88 QMap<int, EncoderLink*> &tvList, 89 RecList &reclist); 90 static void AddNotListed(MSqlQueryInfo &dbConn, 91 const QString &recordTable, 92 RecList &reclist); 93 static void PruneOverlaps(RecList &reclist); 94 static void BuildListMaps(RecList &reclist, 95 QMap<int, RecList> &cardlistmap, 96 QMap<int, RecList> &recordidlistmap, 97 QMap<QString, RecList> &titlelistmap); 98 static bool FindNextConflict(RecList &cardlist, 99 ProgramInfo *p, 100 RecIter &iter); 101 static void MarkOtherShowings(ProgramInfo *p, 102 QMap<int, RecList> &recordidlistmap, 103 QMap<QString, RecList> &titlelistmap); 104 static void MarkShowingsList(RecList &showinglist, 105 ProgramInfo *p); 106 static void BackupRecStatus(RecList &reclist); 107 static void RestoreRecStatus(RecList &reclist); 108 static bool TryAnotherShowing(QMap<int, RecList> &cardlistmap, 109 QMap<QString, RecList> &titlelistmap, 110 QMap<int, RecList> &recordidlistmap, 111 ProgramInfo *p); 112 static void SchedNewRecords(bool schedMoveHigher, 113 RecList &reclist, 114 RecList &retrylist, 115 QMap<int, RecList> &cardlistmap, 116 QMap<int, RecList> &recordidlistmap, 117 QMap<QString, RecList> &titlelistmap); 118 static void MoveHigherRecords(bool schedMoveHigher, 119 RecList &reclist, 120 RecList &retrylist, 121 QMap<int, RecList> &cardlistmap, 122 QMap<int, RecList> &recordidlistmap, 123 QMap<QString, RecList> &titlelistmap); 124 static bool PruneRedundants(RecList &reclist); 125 static void UpdateNextRecord(MSqlQueryInfo &dbConn, 126 RecList &reclist); 127 static bool ChangeRecordingEnd(bool specsched, 128 QMap<int, EncoderLink*> &tvList, 129 ProgramInfo *oldp, 130 ProgramInfo *newp); 102 131 132 private: 133 QString recordTable; 103 134 QValueList<int> reschedQueue; 104 135 QMutex reschedLock; 105 136 QMutex recordmatchLock; 106 137 QWaitCondition reschedWait; 107 138 RecList reclist; 108 RecList retrylist;109 139 RecList schedlist; 110 QMap<int, RecList> cardlistmap;111 QMap<int, RecList> recordidlistmap;112 QMap<QString, RecList> titlelistmap;113 140 114 141 QMutex *reclist_lock; 115 142 QMutex *schedlist_lock;